gpt4 book ai didi

ios - NSMutableDictionary setobject : forkey: crashes with unrecognised selector message

转载 作者:行者123 更新时间:2023-11-29 12:33:53 28 4
gpt4 key购买 nike

对于下面的代码片段:错误发生在 setObject: forKey:

response <--is an NSDictionary

if(response){
NSMutableArray *lifeArray = [[NSMutableArray alloc] init];
NSMutableDictionary *responseDict = [[NSMutableDictionary alloc] init];
responseDict = (NSMutableDictionary *)[response mutableCopy];
[responseDict setObject:@"life" forKey:@"label"]; <-- Error here
}

请注意,变量 responseDictNSMutableDictionary,因为它是通过 mutableCopy 复制的。

错误信息:

2014-11-12 12:16:08.534 Lifetape[84381:508759] -[__NSCFArray setObject:forKey:]: unrecognized selector sent to instance 0x7a2967002014-11-12 12:16:08.536 Lifetape[84381:508759] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray setObject:forKey:]: unrecognized selector sent to instance 0x7a296700'*** First throw call stack:(    0   CoreFoundation                      0x028fbdf6 __exceptionPreprocess + 182    1   libobjc.A.dylib                     0x02557a97 objc_exception_throw + 44    2   CoreFoundation                      0x02903a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277    3   CoreFoundation                      0x0284c9c7 ___forwarding___ + 1047    4   CoreFoundation                      0x0284c58e _CF_forwarding_prep_0 + 14    5   Lifetape                            0x000dfb83 __50-[LTTimeLineTableViewController getTimeIntervals:]_block_invoke + 307    6   Lifetape                            0x0014cedb -[MethodInvoker maybeInvokeCallback] + 395    7   Lifetape                            0x0014d19f -[MethodInvoker receiveResultWithError:andResult:] + 399    8   Lifetape                            0x00143e38 -[MeteorClient(Connection) livedata_result:] + 1544    9   Lifetape                            0x00138674 __54-[MeteorClient(Connection) initConnectionWithOptions:]_block_invoke_3 + 2660    10  Lifetape                            0x0014b2b5 -[MeteorClient didReceiveMessage:] + 197    11  Lifetape                            0x0014eb7d -[ObjectiveDDP webSocket:didReceiveMessage:] + 269    12  Lifetape                            0x0015d986 __30-[SRWebSocket _handleMessage:]_block_invoke + 102    13  libdispatch.dylib                   0x031e941a _dispatch_call_block_and_release + 15    14  libdispatch.dylib                   0x03209e1f _dispatch_client_callout + 14    15  libdispatch.dylib                   0x031f0981 _dispatch_main_queue_callback_4CF + 610    16  CoreFoundation                      0x02855f3e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14    17  CoreFoundation                      0x02814d40 __CFRunLoopRun + 2256    18  CoreFoundation                      0x028141ab CFRunLoopRunSpecific + 443    19  CoreFoundation                      0x02813fdb CFRunLoopRunInMode + 123    20  GraphicsServices                    0x051a524f GSEventRunModal + 192    21  GraphicsServices                    0x051a508c GSEventRun + 104    22  UIKit                               0x00f41e16 UIApplicationMain + 1526    23  Lifetape                            0x000ce36d main + 141    24  libdyld.dylib                       0x03235ac9 start + 1)libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

这里你的响应字典是一个 NSDictionary 类型的集合,所以它没有任何像 setObject 这样的选择器,这就是为什么它给你这个错误使用

if(response &&  [response isKindOfClass:[NSDictionary class]]){
NSMutableArray *lifeArray = [[NSMutableArray alloc] init];
NSMutableDictionary *responseDict = [NSMutableDictionary dictionaryWithDictionary:response];
[responseDict setObject:@"life" forKey:@"label"];
}

它会起作用,但要确保 response 是您的字典类型对象。因为您发布的错误清楚地表明您的响应是一个数组而不是字典。

关于ios - NSMutableDictionary setobject : forkey: crashes with unrecognised selector message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26880988/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com