Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting NSDictionary to NSArray

Whats the difference between, these two syntax to convert NSDictionary to NSArray?

//replyInfo is NSDictionary

     NSArray *values=[[NSArray alloc]init];
     values = [replyInfo valueForKey:@"response"];//when this?
     values = [replyInfo allValues];//when this?
like image 752
DEEP PRADHAN 12BIT0052 Avatar asked Mar 08 '16 04:03

DEEP PRADHAN 12BIT0052


People also ask

How do you convert NSDictionary to NSMutableDictionary?

Use -mutableCopy . NSDictionary *d; NSMutableDictionary *m = [d mutableCopy]; Note that -mutableCopy returns id ( Any in Swift) so you will want to assign / cast to the right type. It creates a shallow copy of the original dictionary.

What is a NSDictionary object?

An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.


1 Answers

values = [replyInfo valueForKey:@"response"];

This gives you value for particular key from dictionary.

Where in values = [replyInfo allValues]; returns a new array containing the dictionary’s values

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

When to use:

  • When you want to access particular item from dictionary go for

    values = [replyInfo valueForKey:@"response"];
    
  • When you want to do something with all values like iterate through all values or somethning go for

    values = [replyInfo allValues];
    
like image 107
iAnurag Avatar answered Sep 17 '22 23:09

iAnurag