2013-10-05 41 views
3

我嘗試檢查我的coredata存儲中是否有任何數據作爲我的應用程序的恢復類型。基本上,如果用戶處於最終視圖中,那麼他們會不斷更新coredata中的一些數據。Coredata錯誤setObjectForKey:對象不能爲零

因此,他們是在最後的看法,然後應用程序休息或他們把它睡覺,然後應用程序從內存中刪除。

當應用程序下次加載我檢查我的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)' 

任何幫助,將不勝感激。

+0

什麼包含project.proj?錯誤是說不能爲零? – RFG

+0

您的實體名稱錯誤,當您正在使用'@「Project」'製作'NSEntityDescription'時。它應該是'ProjectList'。 – TheTiger

回答

2

可能是你的代碼必須是:

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)改變。

+0

壞名字conventon的perals。我有另一個名爲ProjectList的coredataobject ..你的答案確實是正確的。謝謝您的幫助 – HurkNburkS

0

這意味着,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]取代nil值,這樣你就可以從字典中刪除這些 與

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

如果有幾個值有檢查所有值的方法嗎?或者如果satment,每行必須是? – HurkNburkS

+0

@HurkNburkS:查看更新的答案。 –

相關問題