CoreData应用程序目录在iOS3上崩溃

时间:2010-12-21 07:25:58

标签: iphone xcode ipad core-data

我几乎完成了我在iOS4上运行良好的应用程序,但是当它加载到3.2模拟器时它会崩溃..

2010-12-21 07:54:32.052 App[14044:207] *** -[NSPathStore2 URLByAppendingPathComponent:]: unrecognized selector sent to instance 0x4d2b640
2010-12-21 07:54:32.054 App[14044:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPathStore2 URLByAppendingPathComponent:]: unrecognized selector sent to instance 0x4d2b640'

我正在使用

获取应用程序文档目录
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"App.sqlite"];

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

任何帮助都会很棒,谢谢。

3 个答案:

答案 0 :(得分:9)

用于

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"app.sqlite"];
NSURL *storeURL = [NSURL fileURLWithPath:myPathDocs];

感谢Justin

答案 1 :(得分:5)

The documentation提到URLByAppendingPathComponent:仅适用于iOS 4及更高版本。您可以将NSStringsstringByAppendingPathComponent:一起使用,然后在完成后转换为网址,从而完成相同的操作。

显然URLsForDirectory:inDomains:也是4.0。查看NSSearchPathsForDirectoriesInDomains()以获取与早期操作系统兼容的替代方案。

答案 2 :(得分:1)

对于iOS3兼容性,请将方法和调用者更改为:

- (NSString *)applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

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

模型也需要改变:

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil) {
    return __managedObjectModel;
    }

    NSString *path = [[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"];  
    NSURL *modelURL = [NSURL fileURLWithPath:path];

//  this was:    
//  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"App" withExtension:@"momd"];

    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}