【问题标题】:What's wrong with my copy here?我这里的副本有什么问题?
【发布时间】:2011-11-06 10:52:39
【问题描述】:

我正在尝试将文件从我的应用程序包复制到我的应用程序的文档目录。我收到一个错误“可可错误 262”。我究竟做错了什么?这是我的代码:

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData.sqlite"];
NSURL *initialURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"sqlite"]];

NSError *error = nil;

if (![[NSFileManager defaultManager] fileExistsAtPath:[initialURL absoluteString]]) {
    NSLog(@"Original does not exist. \nPath: %@", [initialURL absoluteString]);
}  

if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL absoluteString]]) {
    NSLog(@"Destination file does not exist. \nPath: %@", [storeURL absoluteString]);

    [[NSFileManager defaultManager] copyItemAtURL:initialURL toURL:storeURL error:&error];

    NSLog(@"Error: %@", [error description]);
}

【问题讨论】:

    标签: iphone objective-c core-data copy nsfilemanager


    【解决方案1】:

    你得到的错误是

    NSFileReadUnsupportedSchemeError Read error because the specified URL scheme is unsupported

    我认为这意味着您的一条路径没有正确形成。也许将这些路径写入日志,看看它们是否如您所愿。

    【讨论】:

    • 什么是正确的方案,你从哪里得到这些信息?
    • 不要使用 +URLWithString: 除非你想构建整个“file:///path/to/file”路径。但是,当 +fileURLWithPath: 为您执行此操作时,您为什么要这样做。
    • link 获得信息正确的方案几乎意味着您的网址是如何格式化的。
    【解决方案2】:

    在 FoundationErrors.h 中将错误 262 定义为 NSFileReadUnsupportedSchemeError。

    我建议您使用 NSLog() 写出您正在使用的两个 URL 的字面值,并确保它们是 file:// URL 并且它们看起来完整。

    【讨论】:

      【解决方案3】:

      我已经解决了这个问题,但老实说,我不确定它是什么。我必须再次检查工作代码,但这里是:

          NSError *error = nil;
      
      
      NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
      NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", @"CoreData", @"sqlite"]];
      
      //if file does not exist in document directory, gets original from mainbundle and copies it to documents.
      
      if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
          NSString *defaultFilePath = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"sqlite"];
          [[NSFileManager defaultManager] copyItemAtPath:defaultFilePath toPath:filePath error:&error];
      
          if (error != nil) {
              NSLog(@"Error: %@", error);
          }
      }
      

      编辑:

      我怀疑应用程序目录的路径不正确,因为生成的 applicationDocumentsDirectory 的主体看起来与上面显示的 documentsDorectory 变量的值所使用的方法不同。

      【讨论】:

        【解决方案4】:

        问题是您正在使用普通的旧文件路径初始化 URL。

        NSURL *initialURL = 
            [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"CoreData" 
                                                                 ofType:@"sqlite"]];
        

        请改用[NSURL fileURLWithPath:]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-10-03
          • 1970-01-01
          • 1970-01-01
          • 2011-01-22
          • 2016-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多