【问题标题】:coredata problem nsurl may not respond to stringByAppendingPathComponentcoredata 问题 nsurl 可能无法响应 stringByAppendingPathComponent
【发布时间】:2011-06-25 07:03:23
【问题描述】:

在使用 xcode 3.2.5 开始一个新的 coredata 项目后,我遇到了一些问题...我以前使用核心数据的项目(在以前的 xcode 中)运行良好,所以我不知道有什么区别??

所以我在构建并转到调用核心数据的视图时遇到的错误是>

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '***  -[NSURL initFileURLWithPath:]: nil string parameter'

奇怪的是,在我的 *AppDelegate.m 中(编辑感谢 Rog 但仍然无法正常工作!)

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator_ != nil) {
    return persistentStoreCoordinator_;
}   

NSString *storePath = [[self applicationDocumentsDirectory]   stringByAppendingPathComponent: @"staff.sqlite"];

NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; //new position for the storeUrl!    

// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"staff" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"staff.sqlite"];

我收到警告

NSURL may not respond to '-stringByAppendingPathComponent'

I 选项 + 单击此 stringByAppendingPathComponent 并获取(未找到符号!!!!)

但在其他项目中,我会执行选项 + 单击相同的操作并获取定义!

  • 那么这个警告与我的错误有关吗??
  • 如何解决???

编辑, 将此包含在我的 viewDidLoad 中

NSLog(@"path= %@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]) ;

这给了我控制台:

path= /Users/mkss9/Library/Application Support/iPhone Simulator/4.2/Applications/2F364C20-2B87-4ABB-AA3E-FB6F7C15096F/Documents

拜托!我快疯了!!

谢谢!

【问题讨论】:

    标签: iphone core-data nsurl


    【解决方案1】:

    一些 SDK 版本之前(我不确定他们是什么时候做的)苹果在他们的项目模板中更改了 applicationDocumentsDirectory 的返回类型。

    当你创建一个新项目时,它看起来像这样:

    /**
     Returns the URL to the application's Documents directory.
     */
    - (NSURL *)applicationDocumentsDirectory {
        return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    }
    

    在旧模板中它看起来像这样:

    /**
     Returns the path to the application's documents directory.
     */
    - (NSString *)applicationDocumentsDirectory {
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        return basePath;
    }
    

    在这两者之间看起来像这样:

    /**
     Returns the path to the application's Documents directory.
     */
    - (NSString *)applicationDocumentsDirectory {
        return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    }
    

    所以你必须小心,因为所有依赖于 applicationDocumentsDirectory 返回 NSString 的旧代码都不适用于较新的模板。

    您不能只用旧版本替换新版本,因为这会导致您的核心数据方法发生变化。

    所以我建议您编写自己的方法来返回文档目录。 Apple 经常更改他们的applicationDocumentsDirectory

    【讨论】:

      【解决方案2】:

      首先你需要确保你的applicationDocumentsDirectory 方法返回一个NSString。

      一旦解决了,随后的崩溃是因为您传递了一个尚不存在的路径和文件名。

      因此,如果您将NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 移动到检查现有文件的代码之后并放置一个默认文件以防它不存在,它应该可以解决您的问题。

      【讨论】:

      • 嗨,按照你的建议做了,但还没有工作,请查看编辑中的更改,谢谢
      • 所以我想applicationDocumentsDirectory,正在返回字符串??当我在 viewDidLoad 中看到路径时?对于applicationDocumentsDirectory,是这样吗?
      • 您能否在您的persistentStoreCoordinator 方法中发布您的applicationDocumentsDirectory 接口声明和实现,以及NSLogging storePath 的结果?此外,如果您的编辑正确,您没有将 NSURL *storeUrl 方法移动到您的方法检查给定路径中的现有文件之后。
      【解决方案3】:

      我想这是因为-applicationDocumentsDirectory 返回一个NSURL * 而不是NSString *

      【讨论】:

      • 我明白了,但为什么这个确切的代码在我以前的项目中有效?甚至苹果的 coredatabooks 示例也使用了 this> NSString *storePath = [[self applicationDocumentsDirectory] ​​stringByAppendingPathComponent: @"CoreDataBooks.sqlite"];那会是什么?谢谢!
      猜你喜欢
      • 2011-11-06
      • 2015-10-17
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多