通过for循环填充NSMutableArray

时间:2013-12-14 01:46:01

标签: objective-c unrecognized-selector

我正在尝试使用for循环填充一个名为数字的数组,数字为1 - 10。然而,由于某些原因,我不知道,一切都崩溃了。

这是我尝试过的:

for (int counter = 0; counter < 10; counter++) {

    if (!numbers)
    {

        self.numbers = [[NSMutableArray alloc]init];

    }

    [numbers addObject: [NSNumber numberWithInt:counter]];

}

NSLog(@"%lu", [numbers count]);

并将数字放入表格单元格中:

cell.textLabel.text = [numbers objectAtIndex:indexPath.row];

我不知道为什么一切都崩溃了,我正在做的事似乎对我来说。

我收到了这个错误:

    2013-12-13 20:52:29.788 TopNumbers[4003:70b] 10
2013-12-13 20:52:29.792 TopNumbers[4003:70b] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000002
2013-12-13 20:52:29.796 TopNumbers[4003:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000002'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010188e795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001015f1991 objc_exception_throw + 43
    2   CoreFoundation                      0x000000010191fbad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010188009d ___forwarding___ + 973
    4   CoreFoundation                      0x000000010187fc48 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001011e89cb -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 46
    6   Foundation                          0x00000001011e94a6 -[NSConcreteMutableAttributedString initWithString:attributes:] + 249
    7   UIKit                               0x00000001003c7497 -[UILabel _setText:] + 85
    8   TopNumbers                          0x0000000100001b40 -[XYZViewController tableView:cellForRowAtIndexPath:] + 352
    9   UIKit                               0x000000010031fb8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
    10  UIKit                               0x0000000100307836 -[UITableView _updateVisibleCellsNow:] + 2297
    11  UIKit                               0x0000000100318381 -[UITableView layoutSubviews] + 207
    12  UIKit                               0x00000001002afb27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
    13  QuartzCore                          0x0000000103df5a22 -[CALayer layoutSublayers] + 151
    14  QuartzCore                          0x0000000103dea589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    15  UIKit                               0x00000001002a4470 -[UIView(Hierarchy) layoutBelowIfNeeded] + 521
    16  UIKit                               0x0000000100360a96 -[UINavigationController _layoutViewController:] + 1077
    17  UIKit                               0x000000010035e374 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 395
    18  UIKit                               0x000000010029534e -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 235
    19  UIKit                               0x0000000100293e44 +[UIViewAnimationState popAnimationState] + 281
    20  UIKit                               0x000000010051355e -[UINavigationTransitionView transition:fromView:toView:] + 2504
    21  UIKit                               0x0000000100512b94 -[UINavigationTransitionView transition:toView:] + 25
    22  UIKit                               0x0000000100361797 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2893
    23  UIKit                               0x0000000100361a47 -[UINavigationController _startDeferredTransitionIfNeeded:] + 547
    24  UIKit                               0x00000001003624f8 -[UINavigationController __viewWillLayoutSubviews] + 43
    25  UIKit                               0x00000001004767e1 -[UILayoutContainerView layoutSubviews] + 202
    26  UIKit                               0x00000001002afb27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
    27  QuartzCore                          0x0000000103df5a22 -[CALayer layoutSublayers] + 151
    28  QuartzCore                          0x0000000103dea589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    29  QuartzCore                          0x0000000103df5956 -[CALayer layoutIfNeeded] + 162
    30  UIKit                               0x000000010034ffc2 -[UIViewController window:setupWithInterfaceOrientation:] + 264
    31  UIKit                               0x000000010028eb4d -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 4360
    32  UIKit                               0x000000010028da3f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
    33  UIKit                               0x000000010028d98f -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
    34  UIKit                               0x000000010028cc9e -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
    35  UIKit                               0x0000000100343d4a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
    36  UIKit                               0x0000000100287a87 -[UIWindow addRootViewControllerViewIfPossible] + 506
    37  UIKit                               0x0000000100287bd5 -[UIWindow _setHidden:forced:] + 275
    38  UIKit                               0x0000000100290ca2 -[UIWindow makeKeyAndVisible] + 51
    39  UIKit                               0x000000010024f0c8 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1449
    40  UIKit                               0x0000000100252be8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
    41  UIKit                               0x0000000100263aab -[UIApplication handleEvent:withNewEvent:] + 3092
    42  UIKit                               0x0000000100263f1e -[UIApplication sendEvent:] + 79
    43  UIKit                               0x00000001002542be _UIApplicationHandleEvent + 618
    44  GraphicsServices                    0x0000000103996bb6 _PurpleEventCallback + 762
    45  GraphicsServices                    0x000000010399667d PurpleEventCallback + 35
    46  CoreFoundation                      0x0000000101810819 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    47  CoreFoundation                      0x00000001018105ee __CFRunLoopDoSource1 + 478
    48  CoreFoundation                      0x0000000101839ab3 __CFRunLoopRun + 1939
    49  CoreFoundation                      0x0000000101838f33 CFRunLoopRunSpecific + 467
    50  UIKit                               0x00000001002524bd -[UIApplication _run] + 609
    51  UIKit                               0x0000000100254043 UIApplicationMain + 1010
    52  TopNumbers                          0x0000000100001fd3 main + 115
    53  libdyld.dylib                       0x0000000101f1d5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

3 个答案:

答案 0 :(得分:1)

UILabel的text属性想要一个字符串,但是你给它一个NSNumber。您想获取数字的字符串值。

答案 1 :(得分:1)

使用此: cell.textLabel.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:indexPath.row]] ;

并且你也说“如果我注释掉[数字addObject:[NSNumber numberWithInt:counter]] ;,它不会崩溃......” 因为如果你注释掉,数字的数量是零,所以从不需要单元格。

答案 2 :(得分:0)

您应该使用:

cell.textLabel.text = [[numbers objectAtIndex:indexPath.row] stringValue];