4

在使用 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'

我选项 + 单击此 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

请!,我快疯了!

谢谢你!

4

3 回答 3

7

一些 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 的旧代码都不适用于较新的模板。

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

所以我建议你编写自己的方法来返回文档目录。苹果applicationDocumentsDirectory经常更换它们。

于 2011-02-11T08:00:59.967 回答
1

我想这是因为-applicationDocumentsDirectory返回 anNSURL *而不是NSString *.

于 2011-02-11T02:03:44.813 回答
0

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

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

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

于 2011-02-11T02:38:37.843 回答