在applicationSupportDirectory中創建directory 。我認為applicationSupportDirectory 不允許用戶看內部數據。因為這樣我才選它,但是好像不是這樣。
請高手告訴我怎麼回事?謝謝
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationDirectory = [paths objectAtIndex:0]; // Get directory
NSString *dataPath = [applicationDirectory stringByAppendingPathComponent:@"drawings"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])
;// success
else
{
NSLog(@"Failed");
}
}
}
打印你得dataPath 你會發現 .../Applications/2D....566A/Library/Application Support/drawings中
Application Support目錄是不存在的,
所以當你createDirectoryAtPath:filePathAndDirectory withIntermediateDirectories:NO 會返回NO
因為withIntermediateDirectories: 意思是是否創建最終目錄的不存在的父目錄,
所以改成createDirectoryAtPath:filePathAndDirectory withIntermediateDirectories:YES就可以創建成功了
官方解釋:
createIntermediates
If YES, this method creates any non-existent parent directories as part of creating the directory in url. If NO, this method fails if any of the intermediate parent directories does not exist. This method also fails if any of the intermediate path elements corresponds to a file and not a directory.