我正在尝试检查我的 coredata 存储中是否有任何数据作为我的应用程序的恢复类型。基本上,如果用户在最终 View 中,coredata 中有一些他们不断更新的数据。
所以他们在最终 View 中然后应用程序中断或他们将其置于 sleep 状态然后应用程序从内存中删除。
下次加载应用程序时,我检查我的 coredata 对象以查看是否有任何值如果有我提示用户告诉他们有未完成的工作您想从您停止的地方继续更新。
如果他们想重新开始,我会转储当前在我的核心数据中的任何内容并允许他们工作。否则我跳转到最后一个 View 加载 coredata 中的数据并允许它们继续工作。
然而,这是发生错误的地方,我像这样检查我的核心数据。
NSMutableArray *checkFinMutableArray = [coreDataController readFin];
if ([checkFinMutableArray count] > 0) {
//Show mesage, recover or not?
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"Selected projects avalible"];
[alert setMessage:@"It appears that you have unfinished projects from a previous session. Would you like to continue working on these projects?"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
[alert show];
}
这就是我的 coredata 对象的样子
- (NSMutableArray *)readFinDimensions {
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (ProjectList *projectList in fetchedObjects) {
NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];
[tempProjectDictionaryArray setObject:project.proj forKey:@"Proj"]; // this is where the ap dies
[tempProjectDictionaryArray setObject:project.desc forKey:@"Desc"];
[projectDictionaryArray addObject:tempProjectDictionaryArray];
}
return projectDictionaryArray;
}
这就是错误的样子
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: Proj)'
任何帮助将不胜感激。
最佳答案
可能是您的代码必须是:
for (ProjectList *projectList in fetchedObjects) {
NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];
[tempProjectDictionaryArray setObject:projectList.proj forKey:@"Proj"]; // this is where the ap dies
[tempProjectDictionaryArray setObject:projectList.desc forKey:@"Desc"];
[projectDictionaryArray addObject:tempProjectDictionaryArray];
}
其中 project.proj 由 projectList.proj 更改(也适用于 .desc)。
关于iphone - 核心数据错误 setObjectForKey : object cannot be nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19195878/