我試圖將用戶生成的照片的縮略圖保存在文檔目錄的子目錄中,但我無法首先創建該子目錄。當我嘗試在文檔目錄中創建子目錄時,出現此錯誤(無此文件或目錄):「沒有這樣的文件或目錄」創建目錄時出現錯誤
Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)"
UserInfo=0x17ef9c90 {NSFilePath=/var/mobile/Applications/FD0CC480-97FE-4E50-931F-5341DB6AD92B/Documents/com.Jonathan.App-Name/707F0431-7A09-4D33-B122-13C48AD4CA53,
NSUnderlyingError=0x17e51520 "The operation couldn’t be completed. No such file or directory"}
我很困惑。我知道該目錄不存在,因爲我試圖創建它!我能想到的唯一情況是文檔目錄不正確,但是直接來自Apple Docs。
我錯誤地試圖創建子目錄?
- (NSURL *)documentsDirectory
{
NSFileManager *sharedFM = [NSFileManager defaultManager];
NSArray *possibleURLs = [sharedFM URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask];
NSURL *appSupportDir = nil;
NSURL *appDirectory = nil;
if ([possibleURLs count] >= 1)
{
// Use the first directory (if multiple are returned)
appSupportDir = [possibleURLs objectAtIndex:0];
}
// If a valid app support directory exists, add the
// app's bundle ID to it to specify the final directory.
if (appSupportDir)
{
NSString *appBundleID = [[NSBundle mainBundle] bundleIdentifier];
appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
}
return appDirectory;
}
- (NSURL *)thumbnailDirectoryWithName:(NSString *)theName
{
NSURL *directory = [[self documentsDirectory] URLByAppendingPathComponent:theName];
NSURL *thumbnailsDirectory = [directory URLByAppendingPathComponent:@"Thumbnails"];
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:[thumbnailsDirectory path]] == NO)
{
[[NSFileManager defaultManager] createDirectoryAtURL:thumbnailsDirectory withIntermediateDirectories:NO attributes:nil error:&error];
if (error)
{
NSLog(@"[Thumbnail Directory] %@", [error description]);
return nil;
}
}
return thumbnailsDirectory;
}
我自己也嘗試createDirectoryAtPath:
和stringByAppendingPathComponent:
與無濟於事。
的NSURL *directory
在thumbnailDirectoryWithName:
返回:
/var/mobile/Applications/FD0CC480-97FE-4E50-931F-5341DB6AD92B/Documents/com.Jonathan.App-Name/707F0431-7A09-4D33-B122-13C48AD4CA53
的文件路徑,707F0431-7A09-4D33-B122-13C48AD4CA53的最後一個組件,是實體的核心數據,這使我的唯一標識唯一命名一個目錄。
任何幫助,不勝感激!
謝謝,這解決了我的問題。我已經有了和丹一樣的代碼,但它仍然沒有工作。將其更改爲「是」後,即可正常工作 – Shruti
@Shruti,沒問題。我很高興這有助於別人。 – Jonathan