0
我在使用Swift 3在iOS中創建臨時目錄時遇到問題。我從FileManager.temporaryDirectory
獲取臨時目錄URL,並嘗試使用FileManager.createDirectory
創建目錄,但該目錄沒有似乎存在,我無法在其中創建文件。我究竟做錯了什麼?無法創建臨時目錄
let fileManager = FileManager.default
let tempDir = fileManager.temporaryDirectory
let tempDirString = String(describing: tempDir)
print("tempDir: \(tempDir)")
print("tempDirString: \(tempDirString)")
if fileManager.fileExists(atPath: tempDirString) {
print("tempDir exists")
} else {
print("tempDir DOES NOT exist")
do {
try fileManager.createDirectory(at: tempDir, withIntermediateDirectories: true, attributes: nil)
print("tempDir created")
if fileManager.fileExists(atPath: tempDirString) {
print("tempDir exists")
} else {
print("tempDir STILL DOES NOT exist")
}
} catch {
print("tempDir NOT created")
}
}
這將產生輸出:
tempDir: file:///private/var/mobile/Containers/Data/Application/D28B9C5E-8289-4C1F-89D7-7E9EE162AC27/tmp/
tempDirString: file:///private/var/mobile/Containers/Data/Application/ D28B9C5E-8289-4C1F-89D7-7E9EE162AC27/tmp/
tempDir DOES NOT exist
tempDir created
tempDir STILL DOES NOT exist
您不必創建目錄,它已經存在。 –
你也應該看看http://stackoverflow.com/questions/16176911/nsurl-path-vs-absolutestring –