setValue:forUndefinedKey this class is not key value coding-compliant for the key

下午開發過程當中遇到一個錯誤,結果被的真慘,從上午 11 點查錯一直查到下午 2 點才找到錯誤的緣由,真的鬱悶的不行。app

關於查錯這麼久,主要的緣由是:
 

1. 本身對 IOS 開發還不熟悉
2. 不知道怎麼獲得錯誤的詳細信息, 連調出來 All Output 面板查看錯誤信息都不知道,真的差勁
3. 有錯誤信息後應該直接去 stackoverflow 找答案。應爲 IOS 開發中文方面的資料和分享實在太少,出錯就要直接上 stackoverflow.ide

 
下面說錯誤,錯誤的詳細信息:
 

2013-07-12 15:49:43.476 MyPad[15944:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<BenefitItemsTableCell 0x6d9bdd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key.'
*** First throw call stack:
(0x1490022 0x127fcd6 0x148fee1 0xd90022 0xd01f6b 0xd01edb 0x420c88 0xd1cd50 0x60471a 0x1491dea 0x13fb7f1 0x60326e 0x475230 0xd886e94 0x65ea1 0x47cc54 0x47d3ce 0x468cbd 0x4776f1 0x420d42 0x1491e42 0x23e679 0x248579 0x1cd4f7 0x1cf3f6 0x25c160 0x3e0e84 0x3e1767 0x3f0183 0x3f0c38 0x3e4634 0x2329ef5 0x1464195 0x13c8ff2 0x13c78da 0x13c6d84 0x13c6c9b 0x3e0c65 0x3e2626 0x1a0ad 0x2535)
terminate called throwing an exception(lldb) 


其中出錯的代碼在:

ui

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCustomerTableCell";

    // 這行代碼運行出錯
    MyTableCell *cell =(MyTableCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    ....
    return cell;
}

 




關於這個奇怪的錯誤,在 stackoverflow 找到了這樣的解釋:


There are a couple of options to resolve this - i'll let you decide which is the most appropriate.


The reason it's failing is because the owner is being passed as nil. You're binding the actionText outlet to the file's owner in IB, but then when loading the nib, the owner is nil. I'd guess that when loading with a nil owner behind the scenes an NSObject is used, which is why you're getting the key/value error.


My previous advice to pass the cell as the owner would also fail as I didn't know how the Nib is constructed; the cell is nil as you've yet to create it (and dequeue is passing nil back, so even pass cell as the owner is still essentially passing nil).


Two options:


Instantiate a new cell in your -cellForRowAtIndexPath:(NSIndexPath *)indexPath implementation, and pass that new cell as the owner (but i'd guess that this isn't the best solution for you)
Or, and I'd suggest this is the better solution, change the binding of actionText in your nib file to the Alert Cell and not the file's owner (You have File's Owner and an Alert Cell - bind the UILabel to the actionText outlet of the Alert Cell, and not the File's owner, which is what's being done at present) - I suspect this is what you want. With that in mind file's owner can become an NSObject again.
------- Original answer kept below as the file's owner class is also a common cause for this error -------


It suggests that you've 'instantiated' an AlertCell in InterfaceBuilder, and you're binding something to actiontext, but the class isn't set to AlertCell, it's still NSObject?


Take a look at the class text box on the identify tab of the tool palette for that object in Interface Builder. The class should be AlertCell, but i'd guess it's still set to NSObject.


As an aside, and feel free to ignore this advice, but there are a couple of extra things i'd encourage you to do, purely from an Objective C expectations/conventions point of view:


Name your files after your class (upper case the first character of the filename).
Prefix your class names; two uppercase characters, typically your initials (i'd name it DWAlertCell, for example).


我這英文,也只能勉強看個大概,最後知道了緣由在那裏:MyTableCell.h 中的 outlet 上面。this

去代碼中一查,果真緣由在這裏。這是由於我 從 StoryBoard 中鏈接好 outlet 後又手工的在 MyTableCell.h 中更改了outlet 的名字,以後從新鏈接了 outlet,原本覺得這樣萬事大吉,但是卻從 StoryBoard 中逐個檢查 Controller 的 outlet  後,發現我重命名的 outlet 那個控件上面以前的 outlet 沒有刪掉,和新添加的  outlet同事存在,鬱悶,這是 XCODE bug 嗎。

刪掉老的 outlet 後,萬事大吉,鬱悶,煩!

因此這樣的錯誤,就去檢查 StoryBoard 中的 outlet。



spa

相關文章
相關標籤/搜索