Convert NSDictionary to NSArray

What is the difference between these two syntaxes for converting NSDictionary to NSArray?

// replyInfo is an NSDictionary

     NSArray *values=[[NSArray alloc]init];
     values = [replyInfo valueForKey:@"response"];//when this?
     values = [replyInfo allValues];//when this?
+4
source share
4 answers
values = [replyInfo valueForKey:@"response"];

This gives you the value for a specific key from the dictionary.

Where in a values = [replyInfo allValues];new array containing dictionary values is returned

values = [replyInfo allKeys]; will return you an array of all keys in the dictionary.

When to use:

  • If you want to access a specific item from the dictionary, go to

    values = [replyInfo valueForKey:@"response"];
    
  • If you want to do something with all values, such as iterating over all values ​​or something else for

    values = [replyInfo allValues];
    
+6
source

allValues: - . ( ). , dict .

valueForKey: - id. , ( NSArray, NSDictionary, NSString ..).

0

Assuming you get a response as follows,

{
"response" : [1, 2, 3, 4, 5]
}

Then

values = [replyInfo allValues];

will put the data as follows.

values[0] = NSArray ([1, 2, 3, 4, 5])
values[1] = nil

Now, to access such data; we can do the following:

array = [values objectAtIndex:0];

What can be given as

array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
0
source
NSArray *values = [[replyInfo valueForKey:@"response"] allValues];

can you try, please.

0
source

All Articles