Accessing temp directory in Swift

前端 未结 6 2254
死守一世寂寞
死守一世寂寞 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:06

    Swift 3 and up

    I think a good way to do this in swift is with an extension on FileManager. This should create a unique temporary folder and return the URL to you.

    extension FileManager{
    
        func createTemporaryDirectory() throws -> URL {
            let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
    
            try createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
            return url
        }
    }
    

提交回复
热议问题