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
I was just about to ask, but I figured out the answer on my own.

Suppose I have an NSDictionary, and some of its values are NSDictionaries themselves, and some of their values are NSDictionaries. If I want to loop through all of the dictionaries, I can simply create a category on NSDictionary that includes a method that is written like this:

Code:
-(void)doSomething
{
// some code here
for (NSDictionary *dictionary in [self allValues])
{
[dictionary doSomething];
}
}
 
That code will only work if every value in self is an NSDictionary. It will fail if there are any other types.

Since you did say "some of its values" rather than "all of its values", this suggests that some of its values won't be NSDictionary, and those will fail.

You should look at isKindOfClass: in the NSObject protocol methods.
 
That code will only work if every value in self is an NSDictionary. It will fail if there are any other types.

Since you did say "some of its values" rather than "all of its values", this suggests that some of its values won't be NSDictionary, and those will fail.

You should look at isKindOfClass: in the NSObject protocol methods.

A debatably better solution would be to check if it responds to the selector you want to call.

The downside is the possibility that you have multiple unrelated selectors that are called the same thing, then you're accidentally using a selector you didn't intend to use on an object that you failed to anticipate would be present.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.