这就是我现在所拥有的:
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
:
要在其中创建用于自动保存文档或其他一些用途的唯一临时目录的目录的名称。
答案 0 :(得分:2)
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
答案 1 :(得分:1)
未使用参数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)
}
}