Suraj Sukale Suraj Sukale - 12 days ago 4
iOS Question

How to get all values of one specific key from an array?

I am developing application using Objective C.

I am getting following array in the response from server side ( i'm using

Get
method of
AFNetworking
).

Response from server: `[{"name":"option1","profile_id":0,"profile_name":"option1"},{"name":"option2","profile_id":0,"profile_name":"option2"},{"name":"option3","profile_id":0,"profile_name":"option3"},{"name":"option4","profile_id":0,"profile_name":"option4"},{"name":"option5","profile_id":0,"profile_name":"option5"},{"name":"option6","profile_id":0,"profile_name":"option6"},{"name":"option7","profile_id":0,"profile_name":"option7"},{"name":"option8","profile_id":0,"profile_name":"option8"},{"name":"option9","profile_id":0,"profile_name":"option9"},{"name":"option10","profile_id":0,"profile_name":"option10"}]`


I want to separate values for the key
name
.

In short, i want output like:
[option1,option2,option3,option4,option5,option6,option7,option8,option9,option10]


For that i tried like following way:

store that response in array (
_arr
). and then try to separate values for key
name
like following way,

NSArray *optionArray = [_arr valueForKey:@"name"];
NSLog(@"%@",optionArray);


but, unfortunately this is not working. Application crashes and gives following logs.

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7abd0600> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
0 CoreFoundation 0x00ae2a14 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x005a3e02 objc_exception_throw + 50
2 CoreFoundation 0x00ae2631 -[NSException raise] + 17
3 Foundation 0x00239098 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x0017a798 _NSGetUsingKeyValueGetter + 105
5 Foundation 0x0017a727 -[NSObject(NSKeyValueCoding) valueForKey:] + 288
6 Foundation 0x001bac40 -[NSFunctionExpression expressionValueWithObject:context:] + 1079
7 Foundation 0x001ba739 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 290
8 Foundation 0x001ba60f -[NSPredicate evaluateWithObject:] + 48
9 Foundation 0x001ba593 _filterObjectsUsingPredicate + 437
10 Foundation 0x001ba360 -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 314
11 Wellness_24x7 0x0008fbc4 __31-[ForthViewController Donating]_block_invoke + 564
12 Wellness_24x7 0x0006c097 __116-[AFHTTPSessionManager dataTaskWithHTTPMethod:URLString:parameters:uploadProgress:downloadProgress:success:failure:]_block_invoke97 + 231
13 Wellness_24x7 0x00080bd5 __72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2132 + 213
14 libdispatch.dylib 0x0330e377 _dispatch_call_block_and_release + 15
15 libdispatch.dylib 0x033319cd _dispatch_client_callout + 14
16 libdispatch.dylib 0x03316f90 _dispatch_main_queue_callback_4CF + 910
17 CoreFoundation 0x00a33fde __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
18 CoreFoundation 0x009f1cd4 __CFRunLoopRun + 2356
19 CoreFoundation 0x009f10e6 CFRunLoopRunSpecific + 470
20 CoreFoundation 0x009f0efb CFRunLoopRunInMode + 123
21 GraphicsServices 0x050c8664 GSEventRunModal + 192
22 GraphicsServices 0x050c84a1 GSEventRun + 104
23 UIKit 0x012ecbfa UIApplicationMain + 160
24 Wellness_24x7 0x0006905a main + 138
25 libdyld.dylib 0x0335ba21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException


Thank you in advance.

Answer

actually your JSON Response started with Array, so do like

NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: data options: KNilOptions error: nil];

NSMutableArray *finalArray = [NSMutableArray array];
for (NSDictionary *temp in ResponseArray) {
[finalArray addObject:temp[@"name"]];
 }

Choice-2 for AFNetworking

   NSMutableArray *finalArray = [NSMutableArray array];
for (NSDictionary *temp in responseObject) {
[finalArray addObject:temp[@"name"]];
 }
Comments