1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSDictionary Enumerations

Discussion in 'Mac Programming' started by North Bronson, Feb 22, 2011.

  1. North Bronson, Feb 22, 2011
    Last edited: Feb 22, 2011

    macrumors 6502

    #1
    I have an NSDictionary and I need to enumerate through the objects, but I don't need the keys. I see these two different ways:
    Code:
        [myDictionary enumerateKeysAndObjectsUsingBlock: ^(id key, id object, BOOL *stop) {
            
            [object doSomething];
            
        }];
        
        [[myDictionary allValues] enumerateObjectsUsingBlock: ^(id object, NSUInteger index, BOOL *stop) {
            
            [object doSomething];
            
        }];
    What's better here? Is it better to stick with the higher-level API that works directly on the dictionary, or tell the API that I'm going to treat the objects like an array?
     
  2. kainjow, Feb 22, 2011
    Last edited: Feb 22, 2011

    Moderator emeritus

    kainjow

    #2
    If all you need are the objects, I'd probably just do:
    Code:
    for (id object in [myDictionary allValues])
    That's even cleaner and easier to read (and 10.5 compatible ;)).
     

Share This Page