Coredata错误setObjectForKey:对象不能为nil [英] Coredata error setObjectForKey: object cannot be nil

查看:110
本文介绍了Coredata错误setObjectForKey:对象不能为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



因此,他们在最终视图中,然后应用程序打破或他们把它睡觉,然后应用程序从内存中删除。



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



如果他们想要开始新的,我转储当前在我的核心数据,并允许他们工作的任何东西。否则我跳到最后一个视图加载了在coredata中的数据,并允许他们继续工作。



但是这是错误发生的地方我检查我的核心数据像这样。

  NSMutableArray * checkFinMutableArray = [coreDataController readFin]; 

if([checkFinMutableArray count]> 0){
//显示消息,恢复与否?
UIAlertView * alert = [[UIAlertView alloc] init];
[alert setTitle:@Selected projects avalible];
[alert setMessage:@看来你从上一个会话中还有未完成的项目。你要继续在这些项目上工作吗?
[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:@ProjectinManagedObjectContext: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]; //这是ap的地方
[tempProjectDictionaryArray setObject:project.desc forKey:@Desc];

[projectDictionaryArray addObject:tempProjectDictionaryArray];
}

return projectDictionaryArray;
}

这是错误的样子

  ***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:'*** setObjectForKey:object不能为nil(key:Proj)'$ b $ 

方案

可能是你的代码必须是:

  for(ProjectList * projectList in fetchedObjects){

NSMutableDictionary * tempProjectDictionaryArray = [[NSMutableDictionary alloc] init];

[tempProjectDictionaryArray setObject:projectList.proj forKey:@Proj]; //这是ap的地方
[tempProjectDictionaryArray setObject:projectList.desc forKey:@Desc];

[projectDictionaryArray addObject:tempProjectDictionaryArray];
}

其中project.proj由projectList.proj(也适用于.desc) 。


I am tryng to check if there is any data in my coredata storage as a type of recovery for my app. Basicly if the user is in the final view there is some data in coredata that they are constantly updating.

So they are in the final view then the app breaks or they put it to sleep then the app gets removed from memory.

when the app is next loaded I check my coredata object to see if there are any values If there are I prompt the user telling them there is unfinished work would you like to pick up from where you left off of continue fresh.

if they want to start fresh i dump anything thats currently in my core data and allow them to work. else I jump to the last view load up the data thats been in coredata and allow them to continue working.

However this is where the error happens I check my coredata like so.

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];
    }

this is what my coredata object looks like

 - (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;
    }

and this is what the error looks like

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: Proj)'

any help would be greatly appreciated.

解决方案

May be your code must be:

    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];
    }

where project.proj is changed by projectList.proj (also for .desc).

这篇关于Coredata错误setObjectForKey:对象不能为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆