【问题标题】:What is causing my "unrecognized selector" exception?是什么导致我的“无法识别的选择器”异常?
【发布时间】:2014-11-23 07:03:02
【问题描述】:

我正在斯坦福大学的 iTunes U 上独立完成 iOS 7 编程课程,因此现在我有一个问题,我不能问老师,我希望这里有人可以帮助我。理想情况下,通过告诉我如何追踪此类问题,但我还将我的整个项目发布在http://talix.homeip.net/2014/Matchismo.zip(因为我认为我不能有用地发布 UI 以便您在这篇文章中像普通文本一样对其进行调试代码),所以只要告诉我问题出在哪里,也将不胜感激!我正在为所有参加过相同课程的人做作业 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

【问题讨论】:

  • 你必须传递一个字体对象,而不是字体名称。
  • 非常感谢,您当然是对的!一旦你指出了有问题的行,我不仅能够修复它,而且能够准确地理解它为什么需要修复。
  • 我会说不是重复的,因为主要问题仍然存在:为什么在调试模式下我的代码没有崩溃?如果它这样做了并因此指出了哪条线是问题所在,我可能自己就能弄清楚而不会打扰你们这些好人。不过,下次我会尽量记住使用无法识别的选择器文本专门搜索此站点,而不仅仅是“无法识别的选择器”,我确实尝试过,当然它不是很有帮助。 ;-)
  • 另外,感谢 Paul Griffiths - 我应该考虑将这么长的异常文本放在代码块中,并且将来会尝试这样做。

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


【解决方案1】:

看来我无法将 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!

我的更长期和更严重的问题仍然存在:为什么当我在调试器中跳过这段代码时没有崩溃,以便我知道要关注哪一行?

【讨论】:

  • 当你踩到它时它并没有崩溃,因为initWithString 没有严格测试它的参数,而只是存储了值。这是因为参数作为“属性”字典而不是单个参数(这会产生编译器警告消息)传递的事实“促进”了这一点。与 Java 相比,Objective-C 及其库在检查参数方面并不是特别严格。
猜你喜欢
  • 1970-01-01
  • 2013-03-31
  • 2018-02-15
  • 2014-12-25
  • 1970-01-01
  • 2011-04-29
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多