【发布时间】:2014-01-03 08:38:30
【问题描述】:
我正在尝试将用户生成的照片的缩略图保存在文档目录的子目录中,但我一开始就无法创建子目录。当我尝试在文档目录中创建子目录时,出现此错误(没有这样的文件或目录):
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:,但没有成功。
thumbnailDirectoryWithName: 中的 NSURL *directory 返回:
/var/mobile/Applications/FD0CC480-97FE-4E50-931F-5341DB6AD92B/Documents/com.Jonathan.App-Name/707F0431-7A09-4D33-B122-13C48AD4CA53
文件路径的最后一个组成部分 707F0431-7A09-4D33-B122-13C48AD4CA53 是 Core Data 中实体的唯一标识符,它使我能够唯一地命名一个目录。
非常感谢任何帮助!
【问题讨论】:
标签: ios nsfilemanager