In some legacy code (yes, this is superceded by blocks now) I have the following lines:
[UIWindow setAnimationDelegate:self];
[UIWindow setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
but that method - animationFinished:finished:context: - is not defined, yet the code doesn't crash. I wanted to know why, so I tried to log what @selector returns, but I don't know how.
NSLog(@"%@", NSStringFromSelector(@selector(animationFinished:finished:context:)));
doesn't help - it will log the string name of the selector regardless of whether that method is actually implemented or not.
You can safely pass nil to many methods, so I presumed that @selector() was returning nil here, yet the following code doesn't log anything:
if (@selector(animationFinished:finished:context:) == nil) {
NSLog(@"SEL is NIL");
}
So it seems I have a non-nil SEL type here, yet no corresponding method. Is there a way to NSLog() a SEL type or can anyone explain what is going on here? Presumably, the implementation of setAnimationDidStopSelector: is able to cope with a SEL that has no corresponding method and just ignores it..
You're already using NSStringFromSelector which is the correct way to log the selector. Whether you create the selector yourself or ask the target class what selector it has is a different matter.
You can also use respondsToSelector: to determine if a specified class can actually handle the method you want to call on it.
Yes, the selector, meaning the SEL object, may well exist even when the actual method has not been implemented.
In Objective-C an object may respond to any selector regardless whether it corresponds to a method or not. Validation of the selector is a run-time effort. (Some restrictions where introduced with ARC however)
If you want to check whether the receiver has it implemented (or more formally whether it responds to the selector or not) use its -(BOOL)respondsToSelector:(SEL)selector method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With