【问题标题】:Why is this method throwing an exception?为什么这个方法会抛出异常?
【发布时间】:2015-10-29 16:19:57
【问题描述】:

Apple 声称我的一种方法引发了异常,但我无法重现它或找出原因。我收到了一个崩溃日志,它清楚地指向了我在下面发布的方法。很明显,这与访问我的字典有关。但据我所知。

This is the method in question

- (NSDictionary*)formatUserParams {
    // Exception is thrown on the line below this
    NSDictionary *pt_data = @{@"pt_phone"       : _phone,
                              @"or_cash"        : (_has_insurance ? @"0" : @"1"),
                              @"pt_email"       : _email,
                              @"pt_country"     : _country,
                              @"pt_state"       : _state,
                              @"pt_address1"    : _address,
                              @"pt_address2"    : _address2,
                              @"pt_city"        : _city,
                              @"pt_uname"       : _name,
                              @"or_regid"       : _device_id,
                              @"pt_deviceid"    : _device_id,
                              @"pt_zip"         : _zip,
                              @"pt_devicetype"  : @"ios"};
    // Server expects pt_data value as string
    NSString *pt_data_string = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:pt_data options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
    pt_data_string = [pt_data_string stringByReplacingOccurrencesOfString:@"\n" withString:@" "];

    return @{@"pt_data" : pt_data_string};
}

来自 Apple 的崩溃日志

Process:             RxToMe [5640]
Code Type:           ARM-64 (Native)
Parent Process:      launchd [1]
OS Version:          iOS 9.0.2 (13A452)
Report Version:      105

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                 0x182aa0f5c __exceptionPreprocess + 124
1   libobjc.A.dylib               0x197673f80 objc_exception_throw + 56
2   CoreFoundation                 0x18298f8e8 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 324
3   CoreFoundation                 0x18298f780 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 64
4   RxToMe                         0x1000d4bac -[User formatUserParams] (in RxToMe) (User.m:213)
5   RxToMe                         0x1000d3e7c -[User registerAccount] (in RxToMe) (User.m:138)
6   RxToMe                         0x1000d25cc -[ConfirmationViewController completeButtonPressed:] (in RxToMe) (ConfirmationViewController.m:175)
7   UIKit                         0x18802e3c8 -[UIApplication sendAction:to:from:forEvent:] + 100
8   UIKit                         0x1881ad3dc -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 168
9   UIKit                         0x18802e3c8 -[UIApplication sendAction:to:from:forEvent:] + 100
10  UIKit                         0x18802e344 -[UIControl sendAction:to:forEvent:] + 80
11  UIKit                         0x188016c6c -[UIControl _sendActionsForEvents:withEvent:] + 416
12  UIKit                         0x188016db8 -[UIControl _sendActionsForEvents:withEvent:] + 748
13  UIKit                         0x18802dc5c -[UIControl touchesEnded:withEvent:] + 572
14  UIKit                         0x18802d88c -[UIWindow _sendTouchesForEvent:] + 804
15  UIKit                         0x188026ac0 -[UIWindow sendEvent:] + 784
16  UIKit                         0x187ff7a10 -[UIApplication sendEvent:] + 248
17  UIKit                         0x187ff5efc _UIApplicationHandleEventQueue + 5348
18  CoreFoundation                 0x182a585a4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
19  CoreFoundation                 0x182a58038 __CFRunLoopDoSources0 + 540
20  CoreFoundation                 0x182a55d38 __CFRunLoopRun + 724
21  CoreFoundation                 0x182984dc0 CFRunLoopRunSpecific + 384
22  GraphicsServices               0x18d92c088 GSEventRunModal + 180
23  UIKit                         0x18805ef44 UIApplicationMain + 204
24  RxToMe                         0x1000d7c90 0x1000c8000 + 64656
25  libdyld.dylib                 0x197e928b8 start + 4

【问题讨论】:

    标签: ios iphone appstore-approval


    【解决方案1】:

    您传递到字典中的值之一似乎是nil,这是不允许的。仔细检查字典中的所有各种 ivars(_phone_email 等),并查看在调用此方法时哪些可能为 nil。

    【讨论】:

    • 另外,使用 [NSNull null] 初始化参数并确保不要为它们分配 nil 值可能会很好。 NSNull 将被 NSDictionary 接受。
    • 虽然在某些特定情况下这可能是正确的选择,但我倾向于避免使用 NSNull 进行一般参数初始化。虽然许多 API 检查 nil 值并知道如何正确处理它们(例如,请参阅最近的可空性注释),但传递 NSNull 通常是意外的,并且很容易导致无法识别的选择器异常。
    • 同意。我只推荐它,因为它看起来像 @mike 在那里创建的一个非常简单的 NSObject。
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2015-12-24
    • 2014-09-02
    • 2015-07-22
    • 2021-03-04
    • 2010-11-06
    相关资源
    最近更新 更多