coredata问题nsurl可能无法响应stringByAppendingPathComponent

发布于 2024-10-17 04:45:57 字数 1921 浏览 5 评论 0原文

在使用 xcode 3.2.5 启动新的 coredata 项目后,我遇到了一些问题...我之前使用 core data 的项目(在以前的 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

拜托!,我快疯了!

谢谢你!

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'

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

in the

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

I get the warning

NSURL may not respond to '-stringByAppendingPathComponent'

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!!

  • so is this warning related to my error??
  • how to fix it???

Edit,
included this in my viewDidLoad

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

which gives me in console:

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

please!, Im getting crazy !!

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-10-24 04:45:57

一些 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

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

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.

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

心如狂蝶 2024-10-24 04:45:57

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

I would imagine it's because -applicationDocumentsDirectory returns an NSURL * instead of an NSString *.

晚雾 2024-10-24 04:45:57

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

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

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

Firstly you need to make sure you applicationDocumentsDirectory method is returning a NSString.

Once that's out of the way, the subsequent crash is because you are passing a path and filename that don't exist yet.

So if you move your NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; to after the code that checks for an existing file and puts a default one in case it doesn't exist, it should solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文