NSDictionary - 检查对象是否有任何键

时间:2017-10-05 09:20:53

标签: objective-c json key nsarray nsdictionary

我正在使用Pocket API,它使用我的&#34返回JSON提要;稍后阅读"项目。键/值名称是动态的(它们不断变化),因此为了解析它们,我使用[object allKeys]来获取所有不同的键。然后我遍历该数组并将我需要的所有数据存储在自定义对象中。

我遇到的问题是list对象为空时(即:我执行分页时)。当它为空时,执行[object allKeys]崩溃,因为字典中没有键。

有没有办法检查NSDictionary对象是否包含任何键?

正常list对象结构:

list = {

    1170784230 = {
        "amp_url" = "http://jalopnik.com/jaguar-f-type-svr-this-is-it-in-all-its-575-horsepower-1754489727/amp",
        "excerpt" = "A lot of things have run through my mind when I\U2019ve driven the Jaguar F-Type R. \U201cThis thing desperately needs more horsepower\U201d has never been one of them. But we are in the age of overkill, and our beloved F-Type is about to get a heavy dose of just that. Meet the insane F-Type SVR.",
        "favorite" = "true",
        "given_title" = "",
        "given_url" = "http://jalopnik.com/jaguar-f-type-svr-this-is-it-in-all-its-575-horsepower-1754489727",
        "has_image" = 1,
        "has_video" = 0
    };
};

清空list对象结构:

list = {
};

如何在不崩溃的情况下检测对象是否包含任何键?

谢谢你的时间,Dan。

更新1

以下是崩溃日志:

2017-10-05 10:37:37.864293+0100 TestApp[68108:5683371] -[__NSArray0 allKeys]: unrecognized selector sent to instance 0x604000000050
2017-10-05 10:37:37.867373+0100 TestApp[68108:5683371] [General] An uncaught exception was raised
2017-10-05 10:37:37.867409+0100 TestApp[68108:5683371] [General] -[__NSArray0 allKeys]: unrecognized selector sent to instance 0x604000000050
2017-10-05 10:37:37.867506+0100 TestApp[68108:5683371] [General] (
    0   CoreFoundation                      0x00007fff2f81a0fb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff56106c76 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff2f8b2c34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff2f790510 ___forwarding___ + 1456
    4   CoreFoundation                      0x00007fff2f78fed8 _CF_forwarding_prep_0 + 120
    5   TestApp                           0x000000010006e5c4 __52-[DataManager loadPocketData:pagination:completion:]_block_invoke_2 + 356
    6   TestApp                           0x0000000100041c52 __51-[DataManager loadNetworkCustomRequest:completion:]_block_invoke + 162
    7   CFNetwork                           0x00007fff2e7297bf __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
    8   CFNetwork                           0x00007fff2e729021 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 147
    9   Foundation                          0x00007fff3185bab3 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
    10  Foundation                          0x00007fff3185b915 -[NSBlockOperation main] + 68
    11  Foundation                          0x00007fff31859dc2 -[__NSOperationInternal _start:] + 778
    12  libdispatch.dylib                   0x0000000100432f1f _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x0000000100446a14 _dispatch_block_invoke_direct + 576
    14  libdispatch.dylib                   0x0000000100432f1f _dispatch_client_callout + 8
    15  libdispatch.dylib                   0x0000000100446a14 _dispatch_block_invoke_direct + 576
    16  libdispatch.dylib                   0x00000001004467b4 dispatch_block_perform + 109
    17  Foundation                          0x00007fff31855dd0 __NSOQSchedule_f + 342
    18  libdispatch.dylib                   0x0000000100432f1f _dispatch_client_callout + 8
    19  libdispatch.dylib                   0x00000001004475be _dispatch_continuation_pop + 940
    20  libdispatch.dylib                   0x000000010043e5af _dispatch_async_redirect_invoke + 738
    21  libdispatch.dylib                   0x0000000100434d77 _dispatch_root_queue_drain + 723
    22  libdispatch.dylib                   0x0000000100434a50 _dispatch_worker_thread3 + 116
    23  libsystem_pthread.dylib             0x00000001004af1d2 _pthread_wqthread + 1387
    24  libsystem_pthread.dylib             0x00000001004aec55 start_wqthread + 13
)
2017-10-05 10:37:37.868416+0100 TestApp[68108:5683371] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArray0 allKeys]: unrecognized selector sent to instance 0x604000000050'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff2f81a0fb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff56106c76 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff2f8b2c34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff2f790510 ___forwarding___ + 1456
    4   CoreFoundation                      0x00007fff2f78fed8 _CF_forwarding_prep_0 + 120
    5   TestApp                           0x000000010006e5c4 __52-[DataManager loadPocketData:pagination:completion:]_block_invoke_2 + 356
    6   TestApp                           0x0000000100041c52 __51-[DataManager loadNetworkCustomRequest:completion:]_block_invoke + 162
    7   CFNetwork                           0x00007fff2e7297bf __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
    8   CFNetwork                           0x00007fff2e729021 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 147
    9   Foundation                          0x00007fff3185bab3 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
    10  Foundation                          0x00007fff3185b915 -[NSBlockOperation main] + 68
    11  Foundation                          0x00007fff31859dc2 -[__NSOperationInternal _start:] + 778
    12  libdispatch.dylib                   0x0000000100432f1f _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x0000000100446a14 _dispatch_block_invoke_direct + 576
    14  libdispatch.dylib                   0x0000000100432f1f _dispatch_client_callout + 8
    15  libdispatch.dylib                   0x0000000100446a14 _dispatch_block_invoke_direct + 576
    16  libdispatch.dylib                   0x00000001004467b4 dispatch_block_perform + 109
    17  Foundation                          0x00007fff31855dd0 __NSOQSchedule_f + 342
    18  libdispatch.dylib                   0x0000000100432f1f _dispatch_client_callout + 8
    19  libdispatch.dylib                   0x00000001004475be _dispatch_continuation_pop + 940
    20  libdispatch.dylib                   0x000000010043e5af _dispatch_async_redirect_invoke + 738
    21  libdispatch.dylib                   0x0000000100434d77 _dispatch_root_queue_drain + 723
    22  libdispatch.dylib                   0x0000000100434a50 _dispatch_worker_thread3 + 116
    23  libsystem_pthread.dylib             0x00000001004af1d2 _pthread_wqthread + 1387
    24  libsystem_pthread.dylib             0x00000001004aec55 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

0 个答案:

没有答案