Reputation: 1161
I have a number of NSString objects I want to save into an NSDictionary.
How is an NSDictionary initialized with the keys and do the values need to be set there and then or can the dictionary be setup using just the keys and the objects later set?
Upvotes: 0
Views: 467
Reputation: 150605
To start with here's a link to the NSDictionary documentation, and NSMutableDictionary
You can create a NSMutableDictionary
with just keys by passing [NSNull null]
as the object for that key. It's important to use a mutable dictionary here otherwise you won't be able to change the dictionary after you've created it, and a dictionary that just has keys instead of objects is of little use.
That is what NSNull is for: to provide null values for collection types that don't allow nils.
Upvotes: 3
Reputation: 5611
A dictionary can be initialized both as empty or as a list of (value,key)
pairs.
Initializing the dictionary just with keys and not values is useless/meaningless in Objective-C, since it will be equivalent to a void initialization.
If you want to create a dictionary and want to be able to set objects later, you have to use a NSMutableDictionary
, that has not a static initialization of its data, through its setObject:forKey:
method.
Upvotes: 2