looping through an NSMutableDictionary

前端 未结 6 1100
野趣味
野趣味 2021-01-30 08:02

How do I loop through all objects in a NSMutableDictionary regardless of the keys?

6条回答
  •  野性不改
    2021-01-30 08:38

    1. For simple loop, fast enumeration is a bit faster than block-based loop
    2. It's easier to do concurrent or reverse enumeration with block-based enumeration than with fast enumeration When looping with NSDictionary you can get key and value in one hit with a block-based enumerator, whereas with fast enumeration you have to use the key to retrieve the value in a separate message send

    in fast enumeration

    for(id key in myDictionary) {
       id value = [myDictionary objectForKey:key];
      // do something with key and obj
    }
    

    in Blocks :

    [myDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    
       // do something with key and obj
      }];
    

提交回复
热议问题