0

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

@Fogmeister Sorry I don't follow. Didn't I already mention that?
0

Try this logic

NSObject *collectionIndexString = indexPath;
NSMutableDictionary *tempDict = [myNSMutableArray[row] mutableCopy];
tempDict[@"collectionIndex"] = collectionIndexString;// Setting Key and Value
[myNSMutableArray replaceObjectAtIndex:row withObject:tempDict];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.