TemporaryFile

3.0.0

SwiftTemporaryFile provides functions related to temporary files.
YOCKOW/SwiftTemporaryFile

What's New

Use `FileHandleProtocol`.

2020-01-23T05:17:53Z

TemporaryFile is no longer a subclass of FileHandle.

What is SwiftTemporaryFile?

SwiftTemporaryFile provides functions related to temporary files. It was originally written as a part of SwiftCGIResponder.

Requirements

  • Swift 5 (including compatibility mode for 4 or 4.2)
  • macOS or Linux

Dependencies

Usage

import Foundation
import TemporaryFile

let tmpFile = TemporaryFile()
try! tmpFile.write(contentsOf: "Hello, World!".data(using:.utf8)!)
try! tmpFile.seek(toOffset: 0)
print(String(data: try! tmpFile.readToEnd()!, encoding: .utf8)!) // Prints "Hello, World!"
try! tmpFile.copy(to: URL(fileURLWithPath: "/my/directory/hello.txt"))

/*
You can explicitly close the temporary file by calling `try tmpFile.close()`,
though all of the temporary files are automatically closed at the end of program.
*/
import TemporaryFile

// You can pass a closure:
TemporaryFile { (tmpFile: TemporaryFile) -> Void in
  try! tmpFile.write(contentsOf: "Hello, World!".data(using:.utf8)!)
  // ... 
} // Automatically closed.

License

MIT License.
See "LICENSE.txt" for more information.

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

Last updated: Mon Oct 21 2024 14:02:56 GMT-0900 (Hawaii-Aleutian Daylight Time)