如何使用 URLForDirectory 创建子目录?
How to create a subdirectory using URLForDirectory?
这是我现在拥有的:
let path = try! NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true)
这是 print(path)
的输出:
file:///Users/kuna/Library/Developer/CoreSimulator/Devices/92BD140D-5C14-43C4-80D6-904BB9594ED6/data/Containers/Data/Application/CD9B3821-C536-4B46-B838-259490FD008E/Documents/
在该目录中,我需要创建 images
子目录。
我应该执行什么代码来实现它?
文档说 apriopriateForURL
:
The name of a directory inside of which you want to create a unique temporary directory for autosaving documents or some other use.
未使用参数 appropriateForURL
为目录参数传递 .DocumentDirectory
。
以传统方式进行:
let fileManager = NSFileManager.defaultManager()
do {
let documentURL = try fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let imageDirectoryURL = documentURL.URLByAppendingPathComponent("images")
try fileManager.createDirectoryAtURL(imageDirectoryURL, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
if error.code == 516 {
print("The directory already exists")
} else {
print(error)
}
}
let path = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let imageURL = path?.URLByAppendingPathComponent("images")
try! NSFileManager.defaultManager().createDirectoryAtURL(imageURL!, withIntermediateDirectories: true, attributes: nil)
这是我现在拥有的:
let path = try! NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true)
这是 print(path)
的输出:
file:///Users/kuna/Library/Developer/CoreSimulator/Devices/92BD140D-5C14-43C4-80D6-904BB9594ED6/data/Containers/Data/Application/CD9B3821-C536-4B46-B838-259490FD008E/Documents/
在该目录中,我需要创建 images
子目录。
我应该执行什么代码来实现它?
文档说 apriopriateForURL
:
The name of a directory inside of which you want to create a unique temporary directory for autosaving documents or some other use.
未使用参数 appropriateForURL
为目录参数传递 .DocumentDirectory
。
以传统方式进行:
let fileManager = NSFileManager.defaultManager()
do {
let documentURL = try fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let imageDirectoryURL = documentURL.URLByAppendingPathComponent("images")
try fileManager.createDirectoryAtURL(imageDirectoryURL, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
if error.code == 516 {
print("The directory already exists")
} else {
print(error)
}
}
let path = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let imageURL = path?.URLByAppendingPathComponent("images")
try! NSFileManager.defaultManager().createDirectoryAtURL(imageURL!, withIntermediateDirectories: true, attributes: nil)