coredata问题nsurl可能无法响应stringByAppendingPathComponent
在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些 SDK 版本之前(我不确定他们是什么时候做的),苹果在他们的项目模板中更改了
applicationDocumentsDirectory
的返回类型。当您创建一个新项目时,它看起来像这样:
在旧模板中,它看起来像这样:
在这两个模板之间,它看起来像这样:
所以你必须小心,因为所有依赖于 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:
in older templates it looked like this:
and in between those two it looked like this:
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.我想这是因为
-applicationDocumentsDirectory
返回NSURL *
而不是NSString *
。I would imagine it's because
-applicationDocumentsDirectory
returns anNSURL *
instead of anNSString *
.首先,您需要确保您的
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.