I don't understand the differences between these ways of accessing NSDictionary
values
[my_dict objectForKey:@"field"]
[my_dict valueForKey:@"field"]
my_dict[@"field"]
Can someone tell me ?
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.
You can simply say: myDictionary[myWord] = nextValue; Similarly, to get a value, you can use myDictionary[key] to get the value (or nil). The simplified form mentioned above, usually solves problems OCLint (metric static code analysis).
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.
id is a data type of object identifiers in Objective-C, which can be use for an object of any type no matter what class does it have. id is the final super type of all objects.
In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that when using key-value coding the key must be a string (see Accessing Object Properties ). Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.
Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull. NSDictionary is “toll-free bridged” with its Core Foundation counterpart, CFDictionary. See Toll-Free Bridging for more information on toll-free bridging.
Dictionary that provides mapping from keys to values. Foundation. NSDictionary<TKey,TValue> Foundation. NSMutable Dictionary
Returns the value associated from a key in the dictionary, or null if the key is not found. Returns the value associated from a key in the dictionary, or null if the key is not found. The set of keys for the NSDictionary.
[my_dict objectForKey:@"field"]
is an NSDictionary
method. It accepts any type of object.
[my_dict valueForKey:@"field"]
is KVC method. It accepts only NSString
.
my_dict[@"field"]
is same as objectForKey:
. This is new feature added.
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