“饮料搅拌机”示例头第一个iPhone和iPad开发

问题描述:

我有点新Objective-C,所以我一直在学习头第一个iPhone & iPad开发书。在本书中,在第4-5章中,你会设计一个调酒师的应用程序,它基本上是一张带详细列表的饮料列表的表格视图(它假设所选饮料的成分和方向)。数据来自两个属性列表,一个列表中包含饮料名称的字符串列表,另一个列表中包含饮料名称,方向和成分的字典。“饮料搅拌机”示例头第一个iPhone和iPad开发

我跟着教程和应用程序崩溃,重新检查它,仍然崩溃。我使用4.0.1的Xcode版本和MAC OS X 10.6.8。

这里是例外,我希望你们可以看看在这个简单的应用程序会发生什么。

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:@“名”

- (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; 

} 

错误的第一行的线索就是你已经初始化的内容self.drinks在与indexPath.row对应的索引处为NSString。当检索到objectAtIndex:时,它会传递消息objectForKey:就像它是NSArray一样。

看看你已经初始化self.drinks的代码,并确定你已经做了一个适当的数组,包含适当的内容。在你的情况下,它将是一个NSDictionary * for objectForKey的数组:工作)。

+0

@robmayoff感谢您的错误。我已经编辑了答案。 (如果你已经编辑过它,我会批准它的。) – 2012-07-20 03:42:02

+0

谢谢John!,我没有更新新plist的路径(这是替代“旧”的路径),我有那个Dictionary-String内容问题 – iDaniel89 2012-07-20 14:42:40

调试你的代码,检查饮料是一个NSDictonary数组。