2014-11-12 5 views
1

와 충돌 : 오류가 mutableCopy를 통해 복사하기 때문에 변수 responseDictNSMutableDictionary 것을 setObject: forKey:NSMutableDictionary의 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 
} 

주에서 발생한다.

오류 메시지 :

여기
 
2014-11-12 12:16:08.534 Lifetape[84381:508759] -[__NSCFArray setObject:forKey:]: unrecognized selector sent to instance 0x7a296700 
2014-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 
+0

질문을 정확하고 완전한 오류 메시지로 업데이트하십시오. 세부 사항이 중요합니다. – rmaddy

+0

한번이 줄을 사용하십시오 [[NSMutableDictionary alloc] initWithDictionary : response]; 대신 (NSMutableDictionary *) [response mutableCopy]; –

+0

mutablecopy를 수행하기 전에 if 조건을 작성하십시오. [response isKindOfClass : [NSDictionary class]] –

답변

1

가 setObject를 같은 어떤 선택을하지 않도록 응답 사전이있는 NSDictionary 타입의 모음입니다 당신이 오류 사용을주는 이유, 그건

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

및 그것은 작동하지만 귀하의 사전 타입 객체인지 확인하십시오. 당신이 게시 한 오류가 분명히 귀하의 답변은 사전이 아니라 배열이라고 말했기 때문입니다.

+0

NSMutableDictionary 인 responseDict에서 setObject를 호출하려고합니다. 응답하지 않습니다. mutableCopy는 NSMutableDictionary를 제공합니다. –

+0

형제 당신은 응답이 사전이 아닌 배열이라고 생각합니다. – iHulk

+0

한 번 응답을 분명히 확인합니다. 사전이 아닌 배열이라고 생각합니다. – Smile

0

응답이 NSArray 클래스와 유사하므로 아래 코드를 시도하십시오.

if([response isKindOfClass:[NSArray Class]]){ 
NSLog(@"Array"); 
} 
else if([response isKindOfClass:[NSDictionary Class]]){ 
NSLog(@"Dictionary"); 
} 

이제 로그별로 코드를 관리하십시오.

관련 문제