【问题标题】:NSFileManager: Copy file not successNSFileManager:复制文件不成功
【发布时间】:2013-03-22 07:40:50
【问题描述】:

我想将位于/Library 的文件复制到文件夹/User/Library/AddressBook/Sample/, 我用过:

[[NSFileManager defaultManager] copyItemAtPath: @"/Library/MyFile.mp3" 
                                        toPath: @"/User/Library/AddressBook/Sample/MyFile.mp3" 
                                         error: &error];

但我遇到了一个错误,提示“无法完成操作。没有这样的文件或

目录`

我正在使用越狱的 iPhone。

【问题讨论】:

    标签: iphone jailbreak nsfilemanager


    【解决方案1】:

    目录:

    /User/Library/AddressBook/Sample/
    

    通常在手机上不存在。在尝试将 mp3 文件复制到其中之前,您是否添加了 Sample 子目录?

    使用NSFileManager 方法,我还建议使用错误对象来帮助您调试:

    NSError* error;
    [[NSFileManager defaultManager] copyItemAtPath:@"/Library//MyFile.mp3" toPath: @"/User/Library/AddressBook/Sample/MyFile.mp3" error:&error];
    
    if (error != nil) {
        NSLog(@"Error message is %@", [error localizedDescription]);
    }
    

    另外,您的copyItemAtPath 拼写似乎有错误,但可能只是您的问题,而不是您的代码?无论如何,请仔细检查。

    而且,您的路径中也有一个双斜杠 (//),但我认为这不会伤害您。把它拿出来,打字时要小心:)

    更新

    如果您只是正常运行此应用,但在越狱手机上,您的应用将无法访问这些目录。在越狱手机上正常安装的应用程序仍然是沙盒。越狱不会删除手机上的所有规则。如果您在/Applications 中安装应用程序,就像真正的越狱应用程序一样,那么该代码应该适合您。

    【讨论】:

    • 在将 mp3 文件复制到其中之前,我添加了“Sample”子目录。 抱歉打错了。现在我使用了: NSError* 错误; [[NSFileManager defaultManager] copyItemAtPath:@"/Library/MyFile.mp3" toPath:@"/User/Library/AddressBook/Sample/MyFile.mp3" error:&error]; if (error != nil) { NSLog(@"Error message is %@", [error localDescription]); }
    • @user1561904 好的,您仍然看到错误吗? "Error message is ..." 向您展示了什么?
    • 操作无法完成。没有这样的文件或目录
    • @user1561904,您的应用运行情况如何?您是否只是使用普通的 iOS 流程,使用开发人员配置文件对应用程序进行代码签名,然后从 Xcode 构建和运行?或者,您是通过其他进程在 /Applications/ 中安装应用程序吗?
    • @user1561904,请在 iPhone 的命令行中运行命令ls -al /User/Library/AddressBook/ 和ls -al /Library/,并显示结果。请使用上面的编辑链接将其添加到您的问题中,而不是作为评论。它会在上面更具可读性。谢谢。
    【解决方案2】:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSLog(@"%@",libraryDirectory); // Library path
    NSString *AddressBookPath = [libraryDirectory stringByAppendingPathComponent:@"AddressBook"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:AddressBookPath])
    {        
        NSError* error;
        // Create "AddressBook Dir"
        if([[NSFileManager defaultManager] createDirectoryAtPath:AddressBookPath withIntermediateDirectories:NO attributes:nil error:&error])
        {
            // Create "Sample Dir"
            NSString *samplePath = [AddressBookPath stringByAppendingPathComponent:@"Sample"];
            if (![[NSFileManager defaultManager] fileExistsAtPath:AddressBookPath])
            {                
                NSError* error;
                if([[NSFileManager defaultManager] createDirectoryAtPath:AddressBookPath withIntermediateDirectories:NO attributes:nil error:&error])
                {
                    // Copy Files Now
                    NSError* error;
                    NSString *fromPath = [libraryDirectory stringByAppendingPathComponent:@"MyFile.mp3"];
                    NSString *toPath = [samplePath stringByAppendingPathComponent:@"MyFile.mp3"];
                    [[NSFileManager defaultManager] copyItemAtPath:fromPath toPath:toPath error:&error];
    
                    if (error != nil)
                    {
                        NSLog(@"Error message is %@", [error localizedDescription]);
                    }
                }
            }
        }
        else
        {
            NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
            NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2014-11-11
      • 2011-06-09
      • 2011-09-02
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多