man*_*urt 4 iphone core-data nsurl
在使用xcode 3.2.5启动一个新的coredata项目之后我遇到了一些问题......我之前的核心数据项目(在之前的xcode中)运行正常,所以我不知道有什么区别?
所以当我构建并转到调用核心数据的视图时,我得到的错误是>
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
Run Code Online (Sandbox Code Playgroud)
奇怪的是,在我的*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];
}
}
Run Code Online (Sandbox Code Playgroud)
在里面
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"staff.sqlite"];
Run Code Online (Sandbox Code Playgroud)
我收到了警告
NSURL may not respond to '-stringByAppendingPathComponent'
Run Code Online (Sandbox Code Playgroud)
我选择+单击此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
Run Code Online (Sandbox Code Playgroud)
拜托!我疯了!
谢谢!
一些SDK版本之前(我不确定他们什么时候)苹果改变了applicationDocumentsDirectory他们项目模板的返回类型.
创建新项目时,它看起来像这样:
/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Run Code Online (Sandbox Code Playgroud)
在较旧的模板中,它看起来像这样:
/**
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;
}
Run Code Online (Sandbox Code Playgroud)
在这两者之间它看起来像这样:
/**
Returns the path to the application's Documents directory.
*/
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Run Code Online (Sandbox Code Playgroud)
所以你必须要小心,因为依赖于applicationDocumentsDirectory返回NSString的所有旧代码都不适用于较新的模板.
而且您不能仅使用旧版本替换新版本,因为这会导致核心数据方法发生变化.
所以我建议你编写自己的方法来返回文档目录.Apple applicationDocumentsDirectory经常更换它们.
| 归档时间: |
|
| 查看次数: |
4544 次 |
| 最近记录: |