【问题标题】:NSInvalidArgumentException -[NSNull length]: unrecognized selector sent to instanceNSInvalidArgumentException -[NSNull 长度]:无法识别的选择器发送到实例
【发布时间】:2015-12-24 19:58:51
【问题描述】:

我有以下崩溃报告,但无法找到导致崩溃的地方。因为我在任何地方都使用了 Length 字段。 您对这次崩溃的原因有任何想法吗:

Thread : Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x0000000182b78f5c __exceptionPreprocess
1  libobjc.A.dylib                0x000000019776bf80 objc_exception_throw
2  CoreFoundation                 0x0000000182b7fc6c __methodDescriptionForSelector
3  CoreFoundation                 0x0000000182b7cc14 ___forwarding___
4  CoreFoundation                 0x0000000182a80dcc _CF_forwarding_prep_0
5  UIKit                          0x00000001880da13c -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:]
6  UIKit                          0x00000001880da030 -[UILabel _intrinsicSizeWithinSize:]
7  UIKit                          0x00000001881d5328 -[UILabel intrinsicContentSize]
8  UIKit                          0x00000001881d5188 -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints]
9  UIKit                          0x00000001881d4b9c -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints]
10 UIKit                          0x00000001881d0c3c -[UIView(AdditionalLayoutSupport) updateConstraints]
11 UIKit                          0x00000001881d4940 -[UILabel updateConstraints]
12 UIKit                          0x00000001888f9e8c -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
13 UIKit                          0x00000001888fa0f4 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
14 UIKit                          0x00000001888f9dbc -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
15 UIKit                          0x00000001888fa0f4 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
16 UIKit                          0x00000001888f9dbc -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
17 UIKit                          0x00000001888fa0f4 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
18 UIKit                          0x00000001888f9dbc -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
19 Foundation                     0x0000000183a14aa0 -[NSISEngine withBehaviors:performModifications:]
20 UIKit                          0x00000001881d3958 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:]
21 UIKit                          0x00000001888fa0c8 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
22 UIKit                          0x00000001888f9dbc -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
23 UIKit                          0x00000001888fa0f4 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
24 UIKit                          0x00000001888f9dbc -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
25 UIKit                          0x00000001888fa0f4 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
26 UIKit                          0x00000001888f9dbc -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
27 UIKit                          0x00000001888fa0f4 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:]
28 UIKit                          0x00000001881d483c __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke
29 UIKit                          0x00000001881d3968 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:]
30 UIKit                          0x00000001881d4470 -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]
31 UIKit                          0x00000001888fa798 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded]
32 UIKit                          0x00000001883d1fcc -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine]
33 UIKit                          0x00000001880cb038 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
34 QuartzCore                     0x00000001878d1f14 -[CALayer layoutSublayers]
35 QuartzCore                     0x00000001878ccb20 CA::Layer::layout_if_needed(CA::Transaction*)
36 QuartzCore                     0x00000001878cc9e0 CA::Layer::layout_and_display_if_needed(CA::Transaction*)
37 QuartzCore                     0x00000001878cc07c CA::Context::commit_transaction(CA::Transaction*)
38 QuartzCore                     0x00000001878cbdd0 CA::Transaction::commit()
39 UIKit                          0x00000001880ce0dc _UIApplicationHandleEventQueue
40 CoreFoundation                 0x0000000182b305a4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
41 CoreFoundation                 0x0000000182b30038 __CFRunLoopDoSources0
42 CoreFoundation                 0x0000000182b2dd38 __CFRunLoopRun
43 CoreFoundation                 0x0000000182a5cdc0 CFRunLoopRunSpecific
44 GraphicsServices               0x000000018dbb0088 GSEventRunModal
45 UIKit                          0x0000000188136f60 UIApplicationMain
46 Olgera                         0x000000010016c938 main (main.m:15)
47 libdyld.dylib                  0x0000000197f968b8 start

我也有疑问,如果 UILabel 的长度属性导致这个或者 NSNull 对象是另一种类型的对象?

【问题讨论】:

  • 您似乎在询问我为空的对象的长度,但没有任何代码很难准确地说出。您可以在询问对象的长度之前尝试询问对象是否为空,如下所示:[object isKindOfClass [NSNull class]]
  • @turingtested 是的,我想我应该在使用长度之前添加这个比较。我已经使用了大约 70 次长度 :)。还是谢谢
  • 添加断点和日志消息以定位崩溃。这是我最好的建议。
  • 是否可以在我的项目之外获取 NSNull?我修复了 AFNetworking 的问题,但仍然存在 NSNull。

标签: ios objective-c xcode crash-reports crashlytics


【解决方案1】:

[NSNull null] 是一个特殊的单值值,用于表示不允许nil 的集合类型中的null 值,例如NSArrayNSDictionary

如果您知道某个值(例如来自服务器)可以是 null,您需要在尝试将其视为其他值之前检查它

NSDictionary *data = ...
NSString *name = [data objectForKey:@"name"];
// This will cause a crash if the value of the
// `name` key was `null` since objectForKey:
// will return [NSNull null] 
if ([name length] > 0) { 
 // Do something
}

您要做的是在以任何方式使用它之前检查该值是否不是null

if (name != [NSNull null]) {
  if ([name length] > 0) {
    // Do something
  }
}

【讨论】:

  • 我从 AFNetworking 获得了 NSNull,这导致了我的问题。根本问题是服务器端,正如您所说,发送 null 作为响应。所以我使用了[serializer setRemovesKeysWithNullValues:YES],现在空值将是 nil 而不是 NSNull。谢谢
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 2012-07-16
相关资源
最近更新 更多