Accessing temp directory in Swift

前端 未结 6 2203
死守一世寂寞
死守一世寂寞 2020-12-18 02:13

I was trying to access temp directory in Swift. In Objective-C, I could use the following code to do so:

- (NSString *)tempDirectory {

    NSString *tempDir         


        
6条回答
  •  失恋的感觉
    2020-12-18 03:05

    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)
    
    

提交回复
热议问题