Accessing temp directory in Swift

How about something like :

public extension FileManager {
    func createTempDirectory() throws -> String {
        let tempDirectory = (NSTemporaryDirectory() as NSString).appendingPathComponent(UUID().uuidString)
        try FileManager.default.createDirectory(atPath: tempDirectory,
                                                withIntermediateDirectories: true,
                                                attributes: nil)
        return tempDirectory
    }
}

It doesn't answer your question about char* but it's cleaner...

NSFileManager reference here.

Also check out this SO question regarding unique names.


According to Apple, use of NSTemporaryDirectory is discouraged:

See the FileManager method url(for:in:appropriateFor:create:) for the preferred means of finding the correct temporary directory. For more information about temporary files, see File System Programming Guide.

So instead, you should use FileManager.default.temporaryDirectory

or if you want an unique path:

let extractionPath = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true)