2011-12-11 96 views
1

我已經在覈心數據手冊示例中爲我的應用程序內置了一些核心數據支持。該示例使用日期和字符串。不過,我已經嘗試添加添加和編輯整數值的功能。核心數據檢測字符串還是整數?

//If the value is a string 
if ([[editedObject valueForKey:editedFieldKey] isKindOfClass:[NSString class]]) { 
     textField.hidden = NO; 
     datePicker.hidden = YES; 
     textField.text = [editedObject valueForKey:editedFieldKey]; 
     textField.placeholder = self.title; 
     [textField becomeFirstResponder]; 
    } 
    //If the value is a number 
    else { 
     textField.hidden = NO; 
     datePicker.hidden = YES; 
     textField.text = [[editedObject valueForKey:editedFieldKey] stringValue]; 
     textField.placeholder = self.title; 
     [textField becomeFirstResponder]; 
    } 

第一if語句是示例代碼(不檢查其字符串,我補充說),我增加了else語句運行時,它不是一個字符串,但一個整數。它可以工作,但是現在當我編輯一個字符串時,它會跳過if語句,所以行:if ([[editedObject valueForKey:editedFieldKey] isKindOfClass:[NSString class]])不能以某種方式工作。

希望你能幫上忙。讓我知道如果你需要更多繼續。如果你看一下蘋果的CoreDataBooks例子,我的代碼是一樣的,只是我添加了一個字段,它需要一個整數16.

編輯:

將在第一個if語句並返回po [editedObject valueForKey:EditedFiledKey]斷點時在控制檯中,我得到:Can't print the description of a NIL object.

我認爲這是因爲它在對象被創建之前?當視圖出現時(輸入新字符串的視圖)會發生這種情況。

其在按下保存按鈕,這個代碼運行:

- (IBAction)save { 

    // Set the action name for the undo operation. 
    NSUndoManager * undoManager = [[editedObject managedObjectContext] undoManager]; 
    [undoManager setActionName:[NSString stringWithFormat:@"%@", editedFieldName]]; 

    // Pass current value to the edited object, then pop. 
    if ([[editedObject valueForKey:editedFieldKey] isKindOfClass:[NSString class]]) { 
     [editedObject setValue:textField.text forKey:editedFieldKey]; 
    } 
    else { 
     [editedObject setValue:[NSNumber numberWithInteger:[[textField text] integerValue]] forKey:editedFieldKey]; 
    } 

    [self.navigationController popViewControllerAnimated:YES]; 
} 

當這個運行時,它會跳過第一個if語句和符文else語句,然後崩潰,並顯示錯誤:Unacceptable type of value for attribute: property = "firstName"; desired type = NSString; given type = __NSCFNumber; value = 0.

firstName是我的數據模型中的字符串屬性。我猜是因爲第一個if語句失敗,它的前進期望一個整數?我真的不確定。

+0

哪裏'editedObject'從何而來?也許這個價值不是一個字符串。您應該在該行代碼上設置一個斷點,然後運行'po [editedObject valueForKey:editedFieldKey]'來查看它是什麼。 –

+0

檢查我的編輯,我發佈了一些結果。 –

回答

0

我用更多的Core Data應用程序解決了同樣的問題。我也修改了Core Data Books應用程序。如果您注意到,在原始應用中,他們使用BOOL變量(editingDate)來決定是否顯示日期選擇器。我創建了第二個BOOL變量('editingTextView`),並根據需要編輯的內容更改這些BOOL變量。它可能不是最有效的方式,但編程容易,並且易於遵循Core Data Books中已有的內容。

2

好的,所以根據調試器中的值nil,讓我解釋發生了什麼。在Objective-C中,任何發送到nil對象的消息都不會執行任何操作,然後返回nil(它恰好與0falseNO具有完全相同的內存值)。

所以,你這樣做是:

if ([[editedObject valueForKey:editedFieldKey] isKindOfClass:[NSString class]]) { 

如果editedObjectnil,則valueForKey不會做任何事情,並返回nil。然後你發送isKindOfClassnil這也將無所作爲,並返回nil。在if語句中,nil將評估爲NO,將您發送到else聲明。

如果你這樣做:

textField.text = [[editedObject valueForKey:editedFieldKey] stringValue]; 

editedObjectnil,級聯到stringValue返回nil,因此您要設置文本字段的值nil,這是無效的,並會崩潰您的應用程序。

解決方法是重構您的代碼以檢查nil。這是我會怎麼寫代碼:

// don't do anything for a nil value note this will detect editedObject being nil, or the result of valueForKey: being nil. 
if (![editedObject valueForKey:editedFieldKey]) { 
    return; 
} 

// figure out the string value 
NSString *textFieldValue = [editedObject valueForKey:editedFieldKey]; 
if ([textFieldValue isKindOfClass:[NSNumber class]]) { 
    textFieldValue = [(NSNumber *)textFieldValue stringValue] 
} 

// update the text field 
textField.hidden = NO; 
datePicker.hidden = YES; 
textField.text = textFieldValue; 
textField.placeholder = self.title; 
[textField becomeFirstResponder]; 
+0

認爲這將工作,使很多道理。我會保留這些代碼,因爲它似乎更符合邏輯。不過,我仍然遇到同樣的崩潰和錯誤。 –

相關問題