Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary file path using swift

Tags:

macos

swift

cocoa

How to get a unique temporary file path using Swift/Cocoa on OS X? Cocoa does not seem to provide a function for this, only NSTemporaryDirectory() which returns the path of the temporary directory. Using the BSD mktemp function requires a mutable C-string as argument.

like image 849
tmlen Avatar asked Sep 18 '15 17:09

tmlen


People also ask

What is temporary directory in Swift?

Creating a Temporary Directory If you don't intend to keep the temporary file around, you can use the NSTemporaryDirectory() function to get a path to a temporary directory for the current user. Swift Objective-C. let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)

What is NSTemporaryDirectory?

Returns the path of the temporary directory for the current user.

What is a temp file for share?

Temporary files, as the name suggests, are files that are created to hold data temporarily while a more permanent option is still in the process of being created.

How do I create a directory in Swift?

Swift – Create Directory at Specific Path To create a Directory at specific path in Swift, call createDirectory() function on the file manager object. The following code snippet creates Directory at path “/ab/cd/”. withIntermediateDirectories: true creates intermediate directories if not present.


2 Answers

Apple has been trying to move away from path-as-string and into NSURL. Here's one way:

Swift 3:

let directory = NSTemporaryDirectory() let fileName = NSUUID().uuidString  // This returns a URL? even though it is an NSURL class method let fullURL = NSURL.fileURL(withPathComponents: [directory, fileName]) 

Swift 2:

let directory = NSTemporaryDirectory() let fileName = NSUUID().UUIDString  let fullURL = NSURL.fileURLWithPathComponents([directory, fileName]) 
like image 169
Code Different Avatar answered Sep 18 '22 14:09

Code Different


Here is a possible method to use mkstemp() from Swift 3 and later. URL methods are used to convert between URL instances and C strings representing the file system path:

// The template string: let template = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("file.XXXXXX") as NSURL  // Fill buffer with a C string representing the local file system path.  var buffer = [Int8](repeating: 0, count: Int(PATH_MAX)) template.getFileSystemRepresentation(&buffer, maxLength: buffer.count)  // Create unique file name (and open file): let fd = mkstemp(&buffer) if fd != -1 {      // Create URL from file system string:     let url = URL(fileURLWithFileSystemRepresentation: buffer, isDirectory: false, relativeTo: nil)     print(url.path)  } else {     print("Error: " + String(cString: strerror(errno))) } 

Older code for Swift 2:

// The template string: let template = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("file.XXXXXX")  // Fill buffer with a C string representing the local file system path.  var buffer = [Int8](count: Int(PATH_MAX), repeatedValue: 0) template.getFileSystemRepresentation(&buffer, maxLength: buffer.count)  // Create unique file name (and open file): let fd = mkstemp(&buffer) if fd != -1 {      // Create URL from file system string:     let url = NSURL(fileURLWithFileSystemRepresentation: buffer, isDirectory: false, relativeToURL: nil)     print(url.path!)  } else {     print("Error: " + String(strerror(errno))) } 
like image 25
Martin R Avatar answered Sep 19 '22 14:09

Martin R