“饮料搅拌机”示例首先推出iPhone& iPad开发

时间:2012-07-20 03:12:27

标签: iphone objective-c xcode

我是Objective-C的新手,所以我一直在学习Head First iPhone& iPad开发书。在本书的第4-5章中,您假设要为酒保设计一个应用程序,它基本上是一个包含详细信息列表的饮料列表的表格视图(它假设选择了饮料的成分和方向)。数据来自两个属性列表,一个包含带有饮料名称的字符串列表,另一个是包含饮料名称,方向和成分的字典。

我已按照教程和应用程序崩溃,重新检查并仍然崩溃。我使用的是Xcode版本4.0.1和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:@“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;

}

2 个答案:

答案 0 :(得分:2)

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

查看已初始化self.drinks的代码,并确保您已将其设置为包含适当内容的相应数组。在你的情况下,它将是一个NSDictionary *的数组,用于objectForKey:工作)。

答案 1 :(得分:0)

调试代码并检查drink是一个NSDictionary数组。