Coredata错误setObjectForKey:对象不能为零

我尝试检查我的coredata存储中是否有任何数据作为我的应用程序的恢复types。 基本上,如果用户在最后的视图中有一些数据在coredata,他们不断更新。

所以他们在最后的观点,然后应用程序中断,或者他们把它睡觉,然后应用程序从内存中删除。

当应用程序下次加载时,我检查我的coredata对象,看看是否有任何值如果有我提示用户告诉他们有未完成的工作,你想从你离开的地方继续新鲜的。

如果他们想从头开始,我会转储任何目前在我的核心数据,并允许他们工作。 否则,我跳到最后一个视图加载在coredata中的数据,并让他们继续工作。

然而,这是错误发生的地方我检查我的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)更改。

这意味着project.projnil

 [tempProjectDictionaryArray setObject:project.proj forKey:@"Proj"]; 

一个NS(Mutable)Dictionary 不能存储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"]; 

但用[NSNull null]replacenil值,所以你可以从字典中删除这些

 NSArray *keysForNullValues = [tempProjectDictionaryArray allKeysForObject:[NSNull null]]; [tempProjectDictionaryArray removeObjectsForKeys:keysForNullValues];