2011-08-26 38 views
0

我一直在研究一個應用程序,該應用程序需要關於情緒的用戶評級。我正在使用Core Data來存儲這些數據。最初,我試圖存儲評分和「成就」字符串。我已經在屬性Date(Date類型),dailyRating(類型爲Int16),dailyAccomp(類型爲String)和dailyAccompRating(類型爲Int16)的Core Data中創建了一個名爲「Day」的實體。我的應用程序委託的核心數據persistentStoreCoordinator方法在我的應用程序崩潰在以下stmnt:核心數據持久性商店協調員NSURL錯誤

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SelfEsteemBldr.sqlite"]; 

是我給出的

錯誤[NSPathStore2 URLByAppendingPathComponent:]:無法識別的選擇發送到實例0x4d6f440。

也許有關錯誤發生的一些背景可能會有所幫助。

我的主窗口有一個標籤欄控制器作爲rootViewController。在CD模型(LogViewController)的選項卡中,我在導航控制器中設置了一個tableView控制器。導航欄有一個添加按鈕,它推動一個基本上具有文本框的新視圖,以便用戶輸入相關數據。在該視圖中,有一個帶有完成按鈕的導航欄。用戶完成後,完成按鈕變爲保存按鈕。當我點擊保存按鈕時,應用程序崩潰。保存按鈕是ViewDidLoad中的UIButtonItem。下面是保存按鈕的代碼:我註釋掉的大部分代碼,試圖找到

- (void) performAddNewDay:(id)paramSender{ 

SelfEsteemBldrAppDelegate *delegate = (SelfEsteemBldrAppDelegate *) 
[[UIApplication sharedApplication] delegate]; 

NSManagedObjectContext *context = delegate.managedObjectContext; 

NSLog(@"Past init point in PerformAddNewDay"); 


// Get the values from the text fields 

NSInteger dailyRatingAsInteger = [self.textFieldDailyRating.text integerValue]; 
NSNumber *ddailyRating = [NSNumber numberWithInteger:dailyRatingAsInteger]; 
NSLog(@"Daily Rating Entered is %@", ddailyRating); 

NSString *ddailyAccomplishment = self.textFieldAccomplishment.text; 
NSLog(@"Daily Accomplishment Entered is %@", ddailyAccomplishment); 

NSInteger dailyAccompRatingAsInteger = [self.textFieldAccomplishmentRating.text integerValue]; 
NSNumber *ddailyAccompRating = [NSNumber numberWithInteger:dailyAccompRatingAsInteger]; 
NSLog(@"Daily Accomp Rating Entered is %@", ddailyAccompRating); 


// Create a new instance of Day 
Day *newDay = [NSEntityDescription 
        insertNewObjectForEntityForName:@"Day" 
        inManagedObjectContext:context]; 


if (newDay != nil){ 

    // Set the properties according to the values we retrieved from the text fields 

    newDay.dailyAccomp = ddailyAccomplishment; 
    newDay.dailyRating = ddailyRating; 
    newDay.dailyAccompRating = ddailyAccompRating; 

    NSError *savingError = nil; 

    // Save the new day 
    if ([context save:&savingError] == YES){ 
     // If successful, simply go back to the previous screen 
     [self.navigationController popViewControllerAnimated:YES]; 
    } else { 
     // If we failed to save, display a message 
     [self 
     displayAlertWithTitle:NSLocalizedString(@"Saving", nil) 
     message:NSLocalizedString(@"Failed to save the context", nil)]; 
    } 

} else { 
    // We could not insert a new Day managed object 
    [self 
    displayAlertWithTitle:NSLocalizedString(@"New Day", nil) 
    message:NSLocalizedString(@"Failed To Insert A New Day", nil)]; 
} 

}

UIBarButtonItem *newSaveButton = 
[[UIBarButtonItem alloc] 
initWithTitle:NSLocalizedString(@"Save", nil) 
style:UIBarButtonItemStylePlain 
target:self 
action:@selector(performAddNewDay:)]; 
self.saveButton = newSaveButton; 
[newSaveButton release]; 

的UIButtonItem內performAddNewDay方法看起來是這樣的並且它似乎是

**NSManagedObjectContext *context = delegate.managedObjectContext;** 

也就是說,如果我評論每一個下面包括這個stmnt,應用程序不會崩潰。它什麼都不做,只是「等待」(如預期)。如果我取消註釋這個stmnt,應用程序崩潰。 SelfEsteemBldrAppDelegate使用#進口「SelfEsteemBldrAppDelegate.h」 stmnt還進口

再次,我得到的錯誤是在中SelfEsteemBldrAppDelegate.m核心數據堆棧persistentStoreCoordinator方法發生在崩潰之後:。

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SelfEsteemBldr.sqlite"]; 

我正在給定的錯誤是

[NSPathStore2 URLByAppendingPathComponent:]:無法識別的選擇發送到實例0x4d6f440

所以,畢竟,爲什麼我可能會得到這個消息,以及我能做些什麼來解決它?據我所知,我不應該與Core Data Stack方法交互,所以我不想與這些代碼混在一起。任何幫助將不勝感激。另外,如果我遺漏了您可能需要的任何信息,請告訴我。謝謝。

回答

0

正常情況下,我遇到了這個問題,並遇到這個錯誤出現時managedObjectContext是零...爲了解決這個我已經使用下面的代碼,併爲我工作得很好..

if (managedObjectContext == nil) { 
     managedObjectContext = [(iBountyHunterAppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext]; 
    } 
+0

愚蠢的問題,但這會去哪裏?在應用程序委託PSC方法或PerformAddNewDay方法? – pina0004

+0

您嘗試在'performAddNewDay'處保留斷點,您將遇到斷點將被觸發,因此您必須將此代碼保存在performAddNewDay中...只是儘量避免將初始程序更改爲appdelegate – DShah

+0

好的,非常感謝!我很感激。我嘗試使用以下內容: SelfEsteemBldrAppDelegate * delegate =(SelfEsteemBldrAppDelegate *) [[UIApplication sharedApplication] delegate]; NSManagedObjectContext * context = delegate.managedObjectContext; if(context == nil){context = [(SelfEsteemBldrAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; } 我也嘗試過在「上下文」消息之前,但同樣的事情。 任何見解?我想弄明白這一點並理解它。 – pina0004

相關問題