Asserting object class in Objective C
One has to ask exactly why you want to do this in the first place. You said:
Given how tolerant and dynamic objective c is, I like to make code "abuse proof". Instead of having an unrecognized selector message, I have a message telling me which class the object was expected to be.
If you pass an object to a method that does not match the type declared in the signature, i.e., you do something like this:
- (void)doSomething:(NSArray *)array { /* Do something * }
- (void)doSomethingElse
{
NSString *string = @"my string";
[self doSomething:string];
}
you'll get a warning, so you should know at compile time that the object types don't match.
Of course, this doesn't protect you when using a more generic type, such as id
, but if you're worried about unrecognized selectors, you really should check to see if a object responds to that selector, instead of using an assert:
- (id)doSomething:(id)obj
{
if ([obj respondsToSelector:@selector(calculateSomething)]) {
return [obj calculateSomething];
} else {
NSLog(@"%@ does not respond to calculateSomething", obj);
}
}
Or use protocols to check for the availability of methods.
However, that said, yes, your macro will work for your intended purpose.