Iterating through all of an object's keys

Discussion in 'iOS Programming' started by moonman239, Dec 18, 2013.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Suppose I have an object. This object has keys. I want to iterate through all of the keys to see if each of them has a given value. If I can make a dictionary object that has the same keys and values, then I can get an array of all of the object's keys by calling the dictionary's "allKeys" method.
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Was there supposed to be a question in there, somewhere?
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Yeah. What is the best way to iterate through all of an object's keys, besides creating an NSArray and manually entering the names of all of the keys?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Look at the NSEnumerator class.

    For an example of it, see the NSDictionary method keyEnumerator.

    Other collections, such as NSSet and NSArray, also have methods that return an NSEnumerator.

    Whether an NSEnumerator object is the "best way" depends on several factors. Without more details, "best" is impossible to determine.
     
  5. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #5
    NSFastEnumeration or NSDictionary methods could work for you as well.
    check out the documentation there are a few variations on the theme that might work better for you.
    Code:
    - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
    or
    - (NSArray *)allKeysForObject:(id)anObject
    or
    - (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate
    
    Edit:
    or as chown33 said
     

Share This Page