coredata问题nsurl可能不响应stringByAppendingPathComponent [英] coredata problem nsurl may not respond to stringByAppendingPathComponent

查看:187
本文介绍了coredata问题nsurl可能不响应stringByAppendingPathComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用xcode 3.2.5启动一个新的coredata项目后,我有一些问题...我以前的项目与核心数据(在以前的xcode)工作正常,所以我不知道有什么区别??

I had some problems after starting a new coredata project with the xcode 3.2.5... my previous projects with core data (in previous xcode) worked fine, so I dont know what is the difference??

所以我在构建并访问调用核心数据的视图时得到的错误是

so the error I get when I build and go to the view that calls the core data is>

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

奇怪的是,在我的* AppDelegate.m,在编辑感谢罗,但仍然不工作!)

the strange thing is that in my *AppDelegate.m, in (edited thanks Rog but still not working!)

- (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];
    }
}

b $ b

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

我收到警告

NSURL may not respond to '-stringByAppendingPathComponent'

stringByAppendingPathComponent和get(Symbol not found !!!!)

I option + click this stringByAppendingPathComponent and get (Symbol not found!!!!)

但在其他项目中我做选项+点击相同,得到定义!!

but in other projects I do option + click in the same and get the definition!!


  • 因此,此警告与我的错误相关

  • 如何修复

编辑,
包含在我的viewDidLoad中

Edit, included this in my viewDidLoad


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

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

在控制台:

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

请开始吧!

谢谢!

推荐答案

一些SDK版本之前(我不知道什么时候他们没有)在他们的项目模板中改变了 applicationDocumentsDirectory 的返回类型。

Some SDK Version ago (I don't know for sure when they did) apple changed the return type of applicationDocumentsDirectory in their project templates.

当你创建一个新项目如下所示:

When you create a new project it looks like this:

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

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

in older templates it looked like this:

/**
 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;
}

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

and in between those two it looked like this:

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

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

So you have to be careful, because all the old code that relies on applicationDocumentsDirectory returning a NSString won't work with newer templates.

并且你不能只是替换旧版本的旧版本,因为这将导致你的核心的变化数据方法。

And you can't just replace the new version with the older version because this would result in a change in your core data methods.

因此,我建议您编写自己的方法来返回文档目录。苹果经常改变他们的 applicationDocumentsDirectory

So I would suggest you to write your own method for returning the documents directory. Apple changes their applicationDocumentsDirectory quite often.

这篇关于coredata问题nsurl可能不响应stringByAppendingPathComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆