是什么导致我的“无法识别的选择器”异常?

时间:2014-09-28 23:06:45

标签: ios objective-c cocoa-touch exception unrecognized-selector

我独立完成了斯坦福大学iTunes U上的编程iOS 7课程,因此现在我有一个问题,我不能问老师,我希望有人可以帮助我。理想情况下,通过告诉我如何追踪这类问题,但我也在http://talix.homeip.net/2014/Matchismo.zip发布我的整个项目(因为我认为我不能有用地发布用户界面来调试它在这篇文章中像正常的文本代码),所以只要告诉我具体问题在哪里也会非常感激!对于任何参加同一课程的人来说,我正处于作业3的中间位置。

我有一个非常基本的卡片匹配游戏,可以玩扑克牌或“设置”。扑克牌标签效果很好。只要我按下新游戏按钮或点击卡片(这会导致开始新游戏),“设置”选项卡会以未捕获的异常(下面复制)崩溃。我在我的代码中放了一个断点,以便捕获New Game操作,并逐步完成所有生成的代码。当我踩过我的代码时,这一切似乎都很好。当我到达我的最后一个函数的末尾时,由于操作而被调用,然后我按下XCode中的Continue,这就是它崩溃的时候。鉴于这一事实,我不确定如何追踪导致问题的确切原因。

另一个问题:从异常文本中发布整个调用堆栈是否有用,或者顶部的几行是唯一真正有用的(对于像我这样的初学者)?我想我看到有问题的无法识别的选择器是[__NSCFConstantString _isDefaultFace]:但是我不知道在哪里调用它作为我假设的一部分是我使用的类的类型后面的UI代码(UILabel,等等。)。

以下是整个异常文本:

2014-09-28 18:45:54.337 Matchismo[17292:2714082] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0xc721c
2014-09-28 18:45:54.346 Matchismo[17292:2714082] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0xc721c'
*** First throw call stack:
(
    0   CoreFoundation                      0x01cfddf6 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01987a97 objc_exception_throw + 44
    2   CoreFoundation                      0x01d05a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x01c4e9c7 ___forwarding___ + 1047
    4   CoreFoundation                      0x01c4e58e _CF_forwarding_prep_0 + 14
    5   UIFoundation                        0x0355a1a9 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 2593
    6   UIFoundation                        0x035596b6 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 132
    7   UIFoundation                        0x0356b371 __NSStringDrawingEngine + 6661
    8   UIFoundation                        0x03571c00 -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 1311
    9   UIKit                               0x0068fd5e -[UIButton _intrinsicSizeWithinSize:] + 825
    10  UIKit                               0x00ae4be6 -[UIView(UIConstraintBasedLayout) intrinsicContentSize] + 51
    11  UIKit                               0x00ae51e5 -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 36
    12  UIKit                               0x00ae4f16 -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 505
    13  UIKit                               0x00aed1aa -[UIView(AdditionalLayoutSupport) updateConstraints] + 185
    14  UIKit                               0x0068f7e1 -[UIButton updateConstraints] + 3366
    15  UIKit                               0x00aec614 -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 259
    16  UIKit                               0x00aec861 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 127
    17  UIKit                               0x00aec7dc __UIViewRecursionHelper + 41
    18  CoreFoundation                      0x01bf29b9 CFArrayApplyFunction + 57
    19  UIKit                               0x00aec5af -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 158
    20  UIKit                               0x00aec894 __125-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:]_block_invoke + 43
    21  Foundation                          0x015766f7 -[NSISEngine withBehaviors:performModifications:] + 150
    22  Foundation                          0x01579c4f -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    23  UIKit                               0x00aec3fb -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 64
    24  UIKit                               0x00aec861 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 127
    25  UIKit                               0x00aecdf6 __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke + 105
    26  Foundation                          0x015766f7 -[NSISEngine withBehaviors:performModifications:] + 150
    27  Foundation                          0x01579c4f -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    28  UIKit                               0x00aec3fb -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 64
    29  UIKit                               0x00aeca4c -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 248
    30  UIKit                               0x00aed27e -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 170
    31  UIKit                               0x003eec9e -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 120
    32  UIKit                               0x003eef9c -[UIView(Hierarchy) layoutSubviews] + 57
    33  UIKit                               0x003fc9c0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 608
    34  libobjc.A.dylib                     0x0199d771 -[NSObject performSelector:withObject:] + 70
    35  QuartzCore                          0x0462227f -[CALayer layoutSublayers] + 152
    36  QuartzCore                          0x04616105 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 397
    37  QuartzCore                          0x04615f60 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    38  QuartzCore                          0x04574676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
    39  QuartzCore                          0x04575a3c _ZN2CA11Transaction6commitEv + 392
    40  QuartzCore                          0x0463b789 +[CATransaction flush] + 52
    41  UIKit                               0x0036e0d3 _UIApplicationHandleEventQueue + 2296
    42  CoreFoundation                      0x01c217bf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    43  CoreFoundation                      0x01c172cd __CFRunLoopDoSources0 + 253
    44  CoreFoundation                      0x01c16828 __CFRunLoopRun + 952
    45  CoreFoundation                      0x01c161ab CFRunLoopRunSpecific + 443
    46  CoreFoundation                      0x01c15fdb CFRunLoopRunInMode + 123
    47  GraphicsServices                    0x0415024f GSEventRunModal + 192
    48  GraphicsServices                    0x0415008c GSEventRun + 104
    49  UIKit                               0x00371e16 UIApplicationMain + 1526
    50  Matchismo                           0x000bd80d main + 141
    51  libdyld.dylib                       0x022d0ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:8)

看起来我不能将Hot Licks的评论标记为给出声誉并将其标记为已接受答案的答案,因此我将自己的答案"在这里 - 但他应该得到信用! : - )

问题是我将NSString作为NSAttributedString的NSFontAttributeName而不是UIFont对象传递。这是旧的违规代码:

return [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : color,
                                                                    NSStrokeColorAttributeName : color,
                                                                    NSStrokeWidthAttributeName : strokeWidth,
                                                                    NSFontAttributeName : @"System 12.0"}];

以下是更正后的代码:

UIFont *font = [UIFont boldSystemFontOfSize:12];
return [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : color,
                                                                    NSStrokeColorAttributeName : color,
                                                                    NSStrokeWidthAttributeName : strokeWidth,
                                                                    NSFontAttributeName : font}];

回想起来,这是一个明显的错误,但他们总是不明白?这是我第一次使用NSAttributedString,所以希望将来再也不会犯同样的错误。再次感谢,Hot Licks!

我还有一个更长期和更严肃的问题:为什么当我在调试器中执行此代码时,此代码没有崩溃,以便我知道要关注哪一行?