【问题标题】:Objective-C category for NSMutableDictionary not being respected in iOS10iOS10 中不尊重 NSMutableDictionary 的 Objective-C 类别
【发布时间】:2016-09-14 16:41:29
【问题描述】:

我有一个用 Objective-C 编写的应用程序,其类别为 NSMutableDictionary。在 iOS9 中,该应用程序运行良好。然而,当在 iOS10 中运行时,应用程序由于“发送到实例的无法识别的选择器”而崩溃。我已将问题隔离为在 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

【问题讨论】:

  • 您可以将完整的消息粘贴到您的问题中吗?有时它报告的实例类型和地址提供了线索。
  • id 转换为NSMutableDictionary
  • 以上在iOS 10中没有问题。它工作正常。确保此类别包含在您正在构建的目标中。如果不是这样,请编辑您的问题以包含完整的错误消息。

标签: ios objective-c ios10


【解决方案1】:

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

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

【讨论】:

  • 这个解决方案确实有效,但是由于代码库中出现了 92 次 getObjectForStringKeyPath,我决定采用不同的解决方案。由于某些原因 iOS10 认为对象类型是 NSDictionary,所以我使用 getObjectForStringKeyPath 方法为 NSDictionary 创建了一个类别。
  • 有道理...我假设您需要 NSMutableDictionary 用于某些未知目的。 :)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2012-10-28
    • 2011-01-27
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多