Iphone Coredata错误setObjectForKey:对象不能为零
我正在尝试检查coredata存储中是否有任何数据作为我应用程序的恢复类型。基本上,如果用户处于最终视图中,coredata中的某些数据会不断更新 因此,他们在最终视图中,然后应用程序中断,或者他们将其置于睡眠状态,然后应用程序从内存中删除 下次加载应用程序时,我检查我的coredata对象,查看是否有任何值,如果有,我会提示用户告诉他们,您希望从您停止继续刷新的位置继续未完成的工作 如果他们想重新开始,我会转储我核心数据中当前的任何内容,并允许他们工作。否则,我跳到最后一个视图,加载coredata中的数据,并允许它们继续工作 然而,这就是错误发生的地方,我像这样检查我的coredataIphone Coredata错误setObjectForKey:对象不能为零,iphone,ios,core-data,Iphone,Ios,Core Data,我正在尝试检查coredata存储中是否有任何数据作为我应用程序的恢复类型。基本上,如果用户处于最终视图中,coredata中的某些数据会不断更新 因此,他们在最终视图中,然后应用程序中断,或者他们将其置于睡眠状态,然后应用程序从内存中删除 下次加载应用程序时,我检查我的coredata对象,查看是否有任何值,如果有,我会提示用户告诉他们,您希望从您停止继续刷新的位置继续未完成的工作 如果他们想重新开始,我会转储我核心数据中当前的任何内容,并允许他们工作。否则,我跳到最后一个视图,加载cored
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)'
这就是错误的样子
- (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)'
任何帮助都将不胜感激。这意味着
project.proj
在
[tempProjectDictionaryArray setObject:project.proj forKey:@"Proj"];
NS(可变)字典
无法将nil
存储为值,因此您应该检查例如
if (project.proj != nil) {
[tempProjectDictionaryArray setObject:project.proj forKey:@"Proj"];
}
或者,您可以使用
tempProjectDictionaryArray = [project dictionaryWithValuesForKeys:@[@"Proj", @"Desc"]];
这几乎和
[tempProjectDictionaryArray setObject:project.proj forKey:@"Proj"];
[tempProjectDictionaryArray setObject:project.desc forKey:@"Desc"];
但将nil
值替换为[NSNull null]
,因此您可以删除这些值
从字典里
NSArray *keysForNullValues = [tempProjectDictionaryArray allKeysForObject:[NSNull null]];
[tempProjectDictionaryArray removeObjectsForKeys:keysForNullValues];
可能是您的代码必须是:
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)更改。什么包含project.proj?错误是说不能为零?当您在使用
@“项目”
时,您的实体名称是错误的。它应该是ProjectList
。如果有几个值,有没有办法检查所有值?或者每一行都必须是这样的吗?坏的命名法的例外。我有另一个名为ProjectList的coredataobject。。你的回答确实正确。谢谢你的帮助