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")
}
}
これは出力を生成します。あなたはfileManager.fileExists(atPath: tempDirString)
に渡している
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もご覧ください。 –