[iOS] for each loop in objective c for accessing NSMutable dictionary

Posted in :

如何瀏覽所有NSMutableDictionary 裡的內容?

NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init];

解答:

for (NSString* key in xyz) {
    id value = [xyz objectForKey:key];
    // do stuff
}

This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary is one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumeration in the Collections Programming Topic.

Oh, I should add however that you should NEVER modify a collection while enumerating through it.

 


from:

http://stackoverflow.com/questions/2143372/for-each-loop-in-objective-c-for-accessing-nsmutable-dictionary

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *