Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,544
6,042
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.

Was there supposed to be a question in there, somewhere?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
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.
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
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?

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.