Supertecnoboff
Supertecnoboff

Reputation: 6606

NSDictionary - Check if object has any keys

I am working with the Pocket API, it returns a JSON feed with my "read later" items. The key/value names are dynamic (they keep changing), so in order to parse through them, I am using [object allKeys] to get all the different keys. I then loop through that array and store all the data I need in my custom object.

The problem I am having is when the list object is empty (ie: when I am performing pagination). When it is empty, performing [object allKeys] crashes, because there are no keys in the dictionary.

Is there a way to check if an NSDictionary object, contains any keys or not?

Normal list object structure:

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
    };
};

Empty list object structure:

list = {
};

How can I detect if an object contains any keys without crashing?

Update 1

Here is the crash log:

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) 

Upvotes: 0

Views: 96

Answers (0)

Related Questions