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?
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.
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With