我对 Objective-C 有点陌生,所以我一直在学习 Head First iPhone 和 iPad 开发这本书。在本书的第 4-5 章中,您假设为调酒师设计一个应用程序,它基本上是一个包含详细信息列表的饮料列表的表格视图(假设选择了饮料的成分和说明)。数据来自两个属性列表,一个是包含饮料名称的字符串列表,另一个是包含饮料名称、方向和成分的字典。
我已按照教程进行操作,但应用程序崩溃了,重新检查后仍然崩溃。我正在使用 4.0.1 和 MAC OS X 10.6.8 的 Xcode 版本。
这是一个例外,我希望你们可以看看这个简单的应用程序发生了什么。
2012-07-19 21:43:39.757 DrinkMixer[846:207] -[NSCFString objectForKey:]: unrecognized selector sent to instance 0x4b31220
2012-07-19 21:43:39.761 DrinkMixer[846:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x4b31220'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc25a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f16313 objc_exception_throw + 44
2 CoreFoundation 0x00dc40bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d33966 ___forwarding___ + 966
4 CoreFoundation 0x00d33522 _CF_forwarding_prep_0 + 50
5 DrinkMixer 0x00002495 -[RootViewController tableView:cellForRowAtIndexPath:] + 325
6 UIKit 0x00089b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
7 UIKit 0x0007f4cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
8 UIKit 0x000948cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
9 UIKit 0x0008c90c -[UITableView layoutSubviews] + 242
10 QuartzCore 0x016aca5a -[CALayer layoutSublayers] + 181
11 QuartzCore 0x016aeddc CALayerLayoutIfNeeded + 220
12 QuartzCore 0x016540b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x01655294 _ZN2CA11Transaction6commitEv + 292
14 UIKit 0x000169c9 -[UIApplication _reportAppLaunchFinished] + 39
15 UIKit 0x00016e83 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
16 UIKit 0x00021617 -[UIApplication handleEvent:withNewEvent:] + 1533
17 UIKit 0x00019abf -[UIApplication sendEvent:] + 71
18 UIKit 0x0001ef2e _UIApplicationHandleEvent + 7576
19 GraphicsServices 0x00ffb992 PurpleEventCallback + 1550
20 CoreFoundation 0x00da3944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
21 CoreFoundation 0x00d03cf7 __CFRunLoopDoSource1 + 215
22 CoreFoundation 0x00d00f83 __CFRunLoopRun + 979
23 CoreFoundation 0x00d00840 CFRunLoopRunSpecific + 208
24 CoreFoundation 0x00d00761 CFRunLoopRunInMode + 97
25 UIKit 0x000167d2 -[UIApplication _run] + 623
26 UIKit 0x00022c93 UIApplicationMain + 1160
27 DrinkMixer 0x00001c89 main + 121
28 DrinkMixer 0x00001c05 start + 53
)
terminate called after throwing an instance of 'NSException'
sharedlibrary apply-load-rules all
(gdb)
这正是应用程序崩溃的时间,在objectForKey:@"name"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}