【问题标题】:App crashing due to NSInvalidArgumentException but can't find it由于 NSInvalidArgumentException 导致应用程序崩溃但找不到它
【发布时间】:2014-10-08 19:40:49
【问题描述】:

我的应用在运行以下调试时崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSConcreteMutableAttributedString initWithString:attributes:: nil value'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102a83495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001027e299e objc_exception_throw + 43
    2   CoreFoundation                      0x0000000102a832ad +[NSException raise:format:] + 205
    3   Foundation                          0x00000001023d993e -[NSConcreteMutableAttributedString initWithString:attributes:] + 129
    4   myApp                             0x0000000100006da5 -[WelcomeViewController viewDidLoad] + 373
    5   UIKit                               0x000000010148559e -[UIViewController loadViewIfRequired] + 562
    6   UIKit                               0x0000000101485777 -[UIViewController view] + 29
    7   UIKit                               0x00000001017902e2 -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 390
    8   UIKit                               0x00000001013cbffa -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 1109
    9   UIKit                               0x00000001013cbb9f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
    10  UIKit                               0x00000001013cbaef -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
    11  UIKit                               0x00000001013cadfe -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
    12  UIKit                               0x000000010148970a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
    13  UIKit                               0x00000001013c5b1b -[UIWindow addRootViewControllerViewIfPossible] + 490
    14  UIKit                               0x00000001013c5c70 -[UIWindow _setHidden:forced:] + 282
    15  UIKit                               0x00000001013ceffa -[UIWindow makeKeyAndVisible] + 51
    16  UIKit                               0x000000010138ac98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
    17  UIKit                               0x000000010138ea0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
    18  UIKit                               0x000000010139fd4c -[UIApplication handleEvent:withNewEvent:] + 3189
    19  UIKit                               0x00000001013a0216 -[UIApplication sendEvent:] + 79
    20  UIKit                               0x0000000101390086 _UIApplicationHandleEvent + 578
    21  GraphicsServices                    0x00000001047dd71a _PurpleEventCallback + 762
    22  GraphicsServices                    0x00000001047dd1e1 PurpleEventCallback + 35
    23  CoreFoundation                      0x0000000102a05679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    24  CoreFoundation                      0x0000000102a0544e __CFRunLoopDoSource1 + 478
    25  CoreFoundation                      0x0000000102a2e903 __CFRunLoopRun + 1939
    26  CoreFoundation                      0x0000000102a2dd83 CFRunLoopRunSpecific + 467
    27  UIKit                               0x000000010138e2e1 -[UIApplication _run] + 609
    28  UIKit                               0x000000010138fe33 UIApplicationMain + 1010
    29  myApp                             0x0000000100006b03 main + 115
    30  libdyld.dylib                       0x00000001033c25fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

问题是我知道我从未使用过 NSConcreteMutableAttributedString 的事实我已经检查并仔细检查了我的代码,看看是否可能我遗漏了什么,但我一直空白,任何关于可以做什么的想法解决这个问题?

更新

我添加了一个异常断点,它告诉我以下行导致我的应用程序崩溃:

 NSMutableAttributedString *thirdGreetingAttr = [[NSMutableAttributedString alloc] initWithString:thirdGreeting attributes:secondGreetingAttrs19];
    self.welcomeUserLabel.attributedText = thirdGreetingAttr;

“好吧,奇怪”我心想,两周前我最后一次接触这个项目时,它工作得很好,这是完整的代码块:

 NSDictionary *secondGreetingAttrs19 = [NSDictionary dictionaryWithObjectsAndKeys:fitnestFontBold, NSFontAttributeName, nil];
    NSString *thirdGreeting = currentUser.username;
    NSMutableAttributedString *thirdGreetingAttr = [[NSMutableAttributedString alloc] initWithString:thirdGreeting attributes:secondGreetingAttrs19];
    self.welcomeUserLabel.attributedText = thirdGreetingAttr;

所以我继续注释掉导致问题的 2 行,应用程序运行良好。我取消注释这些行回到它最初的样子,令我惊讶的是,构建运行得很好:/什么???有人知道这里会发生什么吗?

更新

正如所选答案所说,我意识到发生此错误是因为我的 username 属性返回了 null 值。

【问题讨论】:

  • 添加异常断点,看看是哪一行导致崩溃。
  • 我在所有视图控制器上都这样做了,但没有运气
  • 整个项目只需要一个。看第一个答案at this link.
  • 好的,这给了我导致崩溃的行,仍然不明白为什么......但是现在我知道崩溃发生在哪里,谢谢! @rebello95
  • 你能把这条线(以及它所在的功能)贴出来让我们帮忙吗?

标签: ios objective-c


【解决方案1】:

确保您的 thirdGreeting(NSString *thirdGreeting = currentUser.username; ) 不是 nil 值。 我认为 thireGreeting 是 nil,你应该改为

NSString *thirdGreeting = (currentUser.username.length>0) ? currentUser.username.length : @""; 

【讨论】:

  • 这很有趣,我忘记更新问题了,但是在一夜之间经历了几个小时的错误后,我意识到只有当用户名返回 null 时才会发生这种情况,所以我添加了异常,我醒来并找到这个答案,谢谢!
猜你喜欢
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多