【问题标题】:IOS: Moving file to directoryIOS:将文件移动到目录
【发布时间】:2015-09-25 15:44:46
【问题描述】:

我有这个代码和文本错误:

错误:错误域 = NSCocoaErrorDomain 代码 = 4“操作无法完成。(可可错误 4。)” UserInfo = 0x145b5a60 {NSSourceFilePathErrorKey = /private/var/mobile/Containers/Data/Application/E905FE5A-C39B -41F0-8BC7-FC58CC3F4306/库/缓存/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSUserStringVariant=( 移动 ), NSFilePath=/private/var/mobile/Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Library/Caches/2D0404EE-746C-4C1E-98F3-42FFE485BE3B.zip, NSDestinationFilePath=/var/mobile/ Containers/Data/Application/E905FE5A-C39B-41F0-8BC7-FC58CC3F4306/Documents/Files/temp.zip, NSUnderlyingError=0x1457af70 "操作无法完成。没有这样的文件或目录"}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *pathDocumentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dictionaryName = @"Files";
NSString *filesPath = [[pathDocumentDirectory objectAtIndex:0] stringByAppendingPathComponent:dictionaryName];
NSString *toPath = [filesPath stringByAppendingPathComponent:@"temp.zip"];
NSString *fromPath = downloadedItem.contentURL.path;
NSError *error = nil;
BOOL isDir = YES;

if([fileManager fileExistsAtPath:fromPath])
{
    // Create PurchasedBooks directory
    if (![fileManager fileExistsAtPath:dictionaryName isDirectory:&isDir]) {
        [fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];
    }
    [[NSFileManager defaultManager]moveItemAtPath:fromPath toPath:toPath error:&error];

    if (error) {
        NSLog(@"ERROR: %@", error.description);
    }
}

真的不明白为什么会这样

【问题讨论】:

  • [fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error]; 行没有用,因为"Files" 不是文件路径。另外,您是否尝试在操作前记录fromPath 的值?

标签: ios nsfilemanager file-manager


【解决方案1】:

添加错误检查:

[fileManager createDirectoryAtPath:dictionaryName withIntermediateDirectories:NO attributes:nil error:&error];

【讨论】:

  • 错误:错误域 = NSCocoaErrorDomain 代码 = 513“操作无法完成。(可可错误 513。)”用户信息 = 0x165ec390 {NSFilePath =文件,NSUnderlyingError = 0x165ea2d0“操作无法完成完成。不允许操作"}
  • 您需要提供您要创建的文件夹的完整路径作为参数。 @"Files" 似乎不是有效路径。
  • 将 dictionaryName 替换为 createDirectoryAtPath 的 filesPath。
猜你喜欢
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 2016-05-03
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多