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.
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- Using if let syntax in switch statement
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
Apple has been trying to move away from path-as-string and into
NSURL
. Here's one way:Swift 3:
Swift 2:
A Swift 3 one-liner inspired by the UUID based Swift 2 answer:
I like the idea of this article: NSTemporaryDirectory - NSHipster
This uses the
NSTemporaryDirectory()
for the temporary folder andProcessInfo.processInfo.globallyUniqueString
to generate a unique string.Swift 4:
Here is a possible method to use
mkstemp()
from Swift 3 and later.URL
methods are used to convert betweenURL
instances and C strings representing the file system path:Older code for Swift 2:
Although
NSTemporaryDirectory()
does indeed return a temporary directory path for the current user, the documentation includes the following caveat:Following that link, we are presented with the following:
(Note the the
create
parameter is ignored when creating a temporary directory.)So what exactly is the difference between these two approaches? Well, here's what I get when I call the two different methods from the Swift REPL:
It appears that
NSTemporaryDirectory()
will always return the temporary directory path for the current user whereasFileManager
'surl(for:appropriateFor:create)
will return a new temporary subdirectory each time it is called. For example, here are the directories returned by consecutive calls tourl(for:in:appropriateFor:create:)
from the Swift REPL:file:///var/folders/n_/0_9q7d2d1ls5v9kx599y_tj00000gn/T/TemporaryItems/(A%20Document%20Being%20Saved%20By%20repl_swift)/
file:///var/folders/n_/0_9q7d2d1ls5v9kx599y_tj00000gn/T/TemporaryItems/(A%20Document%20Being%20Saved%20By%20repl_swift%202)/
file:///var/folders/n_/0_9q7d2d1ls5v9kx599y_tj00000gn/T/TemporaryItems/(A%20Document%20Being%20Saved%20By%20repl_swift%203)/
And here are the directories returned by consecutive calls to the same method from a Swift Playground:
file:///var/folders/n_/0_9q7d2d1ls5v9kx599y_tj00000gn/T/TemporaryItems/(A%20Document%20Being%20Saved%20By%20Xcode)/
file:///var/folders/n_/0_9q7d2d1ls5v9kx599y_tj00000gn/T/TemporaryItems/(A%20Document%20Being%20Saved%20By%20Xcode%202)/
file:///var/folders/n_/0_9q7d2d1ls5v9kx599y_tj00000gn/T/TemporaryItems/(A%20Document%20Being%20Saved%20By%20Xcode%203)/
The NSHipster article on temporary files seems to suggest that the
FileManager
methodurl(for:in:appropriateFor:create:)
is intended to be used when staging a file to be moved to a more permanent location (such as the user's desktop in the example above), but I don't see why it couldn't also be used to simply get a unique subdirectory that will automatically be removed when you're done with it and where you shouldn't have to worry about files getting accidentally clobbered by other processes writing to the same temporary directory.Use a GUID (Globally Unique Identifier):