gpt4 book ai didi

iphone - coredata问题nsurl可能无法响应stringByAppendingPathComponent

转载 作者:行者123 更新时间:2023-12-03 19:08:03 25 4
gpt4 key购买 nike

在使用 xcode 3.2.5 启动一个新的 coredata 项目后,我遇到了一些问题...我之前使用 core data 的项目(在以前的 xcode 中)运行良好,所以我不知道有什么区别??

所以当我构建并转到调用核心数据的 View 时遇到的错误是>

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

拜托!,我快疯了!!

谢谢!

最佳答案

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

关于iphone - coredata问题nsurl可能无法响应stringByAppendingPathComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4964901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com