Objective-C NSMutableDictionary 类别在 iOS10 中不被尊重

Objective-C category for NSMutableDictionary not being respected in iOS10

我有一个用 Objective-C 编写的应用程序,其中包含一个 NSMutableDictionary 类别。在 iOS9 中,该应用程序运行良好。然而,当 iOS10 中的 运行 时,应用程序因 'unrecognized selector sent to instance' 而崩溃。我已将问题隔离到 iOS10.

中未被尊重的 NSMutableDictionary 类别

NSMutableDictionary 的类别包括以下方法:

- (id)getObjectForStringKeyPath:(NSString *)keysPath {
NSArray *dividedString = [keysPath componentsSeparatedByString:@"/"];
return [self getObjectForKeyPath:dividedString];
}

它崩溃的行是(响应类型 'id'):

NSArray *textArray = [response getObjectForStringKeyPath:"someText/text"];

我已经检查了服务器的响应,响应有效。另外,正如我提到的,崩溃不会发生在 iOS9。它只发生在 iOS10.

有谁知道如何解决这个问题,或者有什么方法可以解决这个问题?

崩溃日志

-[__NSSingleEntryDictionaryI getObjectForStringKeyPath:]: unrecognized selector sent to instance 0x608000a3f3e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleEntryDictionaryI getObjectForStringKeyPath:]: unrecognized selector sent to instance 0x608000a3f3e0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000011036034b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010fdc121e objc_exception_throw + 48
    2   CoreFoundation                      0x00000001103cff34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00000001102e5c15 ___forwarding___ + 1013
    4   CoreFoundation                      0x00000001102e5798 _CF_forwarding_prep_0 + 120
    5   XYZApp                              0x000000010c919b7e __64-[XYZController configureConditionsTextFromHTML:]_block_invoke + 126
    6   XYZApp                              0x000000010c8e973f __41-[XYZDataProvider getTermsAndConditions:]_block_invoke + 159
    7   XYZApp                              0x000000010c8fd5ae -[XYZManager handleSuccessfulResponse:andResponseBlock:] + 110
    8   XYZApp                              0x000000010c8fbede __50-[XYZManager GET:withParameters:andResponseBlock:]_block_invoke + 126
    9   libdispatch.dylib                   0x0000000110872980 _dispatch_call_block_and_release + 12
    10  libdispatch.dylib                   0x000000011089c0cd _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x000000011087ca1d _dispatch_main_queue_callback_4CF + 733
    12  CoreFoundation                      0x00000001103244f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    13  CoreFoundation                      0x00000001102e9f8d __CFRunLoopRun + 2205
    14  CoreFoundation                      0x00000001102e9494 CFRunLoopRunSpecific + 420
    15  GraphicsServices                    0x00000001131c9a6f GSEventRunModal + 161
    16  UIKit                               0x000000010e573f34 UIApplicationMain + 159
    17  XYZApp                              0x000000010c92698f main + 111
    18  libdyld.dylib                       0x00000001108e868d start + 1
    19  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

错误表明对象类型是 __NSSingleEntryDictionaryI,而不是您期望的 NSMutableDictionary

制作服务器提供给您的字典的可变副本,并将 that 分配给 response.

出于某些未记录的原因,iOS10 认为对象类型是 NSDictionary,因此我使用 getObjectForStringKeyPath 方法为 NSDictionary 创建了一个类别。对于我的问题,这是最干净、最有效的解决方案。