Reputation: 197
I found some codes like this:
for (int i = 0 ; i < pinyin_array.count; i++) {
[pinyins_arr addObject:@{k_PINYIN : pinyin_array[i]}];
}
where pinyins_arr is a NSMutableArray
and Kk_PINYIN is a NSString
. I am not sure if this is correct or not. I think it should be a NSMutableDictionary
to add object of key and value pair. If this is true, the same key will be used for multiple values, what is the final array like?
Upvotes: 0
Views: 1513
Reputation: 3291
The @{}
syntax is shorthand for creating an NSDictionary
. The second line could be verbosely re-written as:
[pinyins_arr addObject:[NSDictionary dictionaryWithObject:pinyin_array[i]] forKey:k_PINYIN]
You are adding NSDictionary
objects to your NSMutableArray
, so you will have an array of dictionaries which all have the same key. It is correct, if this is what you want.
Upvotes: 2
Reputation: 481
Try this logic
NSObject *collectionIndexString = indexPath;
NSMutableDictionary *tempDict = [myNSMutableArray[row] mutableCopy];
tempDict[@"collectionIndex"] = collectionIndexString;// Setting Key and Value
[myNSMutableArray replaceObjectAtIndex:row withObject:tempDict];
Upvotes: 0
Reputation: 9731
To say if it's correct or not is not black and white.
It's correct in that it's legal to store an array of dictionaries like this.
It's not correct as there is no need to use an array of dictionaries at all, as there is no variation in the dictionary keys; you may as well just store an array of the values, which is what pinyin_array
is anyway.
Upvotes: 2