insertObject:atIndex 崩溃:对象不能为 nil 但不能为数组

crash with insertObject:atIndex: object cannot be nil but no array

我的应用程序崩溃了,但我找不到它发生的位置。 我正在使用 MZFormSheetController 的最新版本。当我收到一个远程推送时,我展示了一个基本的 MZFormSheetController,它工作得很好,但有一天它开始崩溃并出现奇怪的堆栈跟踪:

Fatal Exception: NSInvalidArgumentException
*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

Thread : Fatal Exception: NSInvalidArgumentException
0  CoreFoundation       0x2bfc2fef __exceptionPreprocess + 126
1  libobjc.A.dylib      0x3a274c8b objc_exception_throw + 38
2  CoreFoundation       0x2bedcefb -[__NSArrayM insertObject:atIndex:] + 638
3  UIKit                0x2fb6289f __workaround10030904InvokeWithTarget_block_invoke + 14
4  UIKit                0x2f8b2ab1 +[UIView _performSystemAppearanceModifications:] + 32
5  UIKit                0x2f780639 workaround10030904InvokeWithTarget + 704
6  UIKit                0x2fb5ff3b applyInvocationsToTarget + 1242
7  UIKit                0x2f6bdd8f +[_UIAppearance _applyInvocationsTo:window:matchingSelector:] + 1262
8  UIKit                0x2f6bd89b +[_UIAppearance _applyInvocationsTo:window:] + 30
9  UIKit                0x2f8bc353 __88-[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:]_block_invoke + 54
10 UIKit                0x2f8bc2cb -[UIView(Internal) _performUpdatesForPossibleChangesOfIdiom:orScreen:traverseHierarchy:] + 262
11 UIKit                0x2f61cf07 -[UIView(Internal) _didChangeFromIdiom:onScreen:traverseHierarchy:] + 30
12 UIKit                0x2f633f0f -[UIScrollView _didChangeFromIdiom:onScreen:traverseHierarchy:] + 46
13 UIKit                0x2f8bc1b7 -[UIView(Internal) _didChangeFromIdiomOnScreen:traverseHierarchy:] + 138
14 UIKit                0x2f61497d -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1316
15 UIKit                0x2f633dfb -[UIScrollView _didMoveFromWindow:toWindow:] + 50
16 UIKit                0x2f6146fb -[UIView(Internal) _didMoveFromWindow:toWindow:] + 674
17 UIKit                0x2f6146fb -[UIView(Internal) _didMoveFromWindow:toWindow:] + 674
18 UIKit                0x2f613fa7 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 114
19 UIKit                0x2f613ec1 -[UIView(Hierarchy) _postMovedFromSuperview:] + 428
20 UIKit                0x2f61e72b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1498
21 UIKit                0x2f61e14b -[UIView(Hierarchy) addSubview:] + 30
22 UIKit                0x2f61db69 -[UIWindow addRootViewControllerViewIfPossible] + 432
23 UIKit                0x2f61b3a1 -[UIWindow _setHidden:forced:] + 272
24 UIKit                0x2f6864a1 -[UIWindow makeKeyAndVisible] + 48
25 Project              0x000f382d -[MZFormSheetController presentAnimated:completionHandler:] (MZFormSheetController.m:509)
26 Project              0x0021b879 __60+[ARDPopupManager showFormsheet:animated:completionHandler:]_block_invoke_2 (ARDPopupManager.m:46)
27 libdispatch.dylib    0x3a7e9ecd _dispatch_barrier_sync_f_slow_invoke + 372
28 libdispatch.dylib    0x3a7df2cf _dispatch_client_callout + 22
29 libdispatch.dylib    0x3a7e2d2f _dispatch_main_queue_callback_4CF + 1330
30 CoreFoundation       0x2bf88609 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
31 CoreFoundation       0x2bf86d09 __CFRunLoopRun + 1512
32 CoreFoundation       0x2bed3201 CFRunLoopRunSpecific + 476
33 CoreFoundation       0x2bed3013 CFRunLoopRunInMode + 106
34 GraphicsServices     0x337b2201 GSEventRunModal + 136
35 UIKit                0x2f677a59 UIApplicationMain + 1440
36 Project              0x0007a88d main (main.m:14)
37 libdyld.dylib        0x3a800aaf start + 2

我当然理解崩溃,但异常断点停在这一行:

[self.formSheetWindow makeKeyAndVisible];

在 MZFormSheetController 库中,MZFormSheetController 是从我的代码中呈现的:

[formsheet presentAnimated:animated completionHandler:nil];

我添加了一个 All Exception 断点,但它没有指向 addObject:insertObject:atIndex: 方法。也许它在其他地方,但我无法弄清楚。我找不到导致此崩溃的 NSArray。

我认为它会解决问题。

YourViewController *objVc = [[YourViewController alloc] init]; // or you can use initWithNib
// Your remaining code

MZFormSheetController *formSheet = [[MZFormSheetController alloc]initWithViewController:objVc];
formSheet.transitionStyle = MZFormSheetTransitionStyleSlideFromBottom; //try different style
formSheet.presentedFormSheetSize = CGSizeMake(320, self.view.frame.size.height);
formSheet.willPresentCompletionHandler = ^(UIViewController *presentedFSViewController)
{
    presentedFSViewController.view.autoresizingMask = presentedFSViewController.view.autoresizingMask | UIViewAutoresizingFlexibleWidth;
};
formSheet.shouldCenterVertically = YES;
[formSheet presentAnimated:YES completionHandler:^(UIViewController *presentedFSViewController){ }];

终于找到了:在以 sheet 控制器形式呈现的视图控制器中,我添加了一个 UITextView,在我的 AppDelegate 中,我写了 [[UITextView appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];,当我呈现 UITextView 时它崩溃了。所以我必须在每个 UITextView 实例故事板或代码上单独设置键盘外观...