开发者

How to find out if a method exists in Obj-C when given an NSString

开发者 https://www.devze.com 2022-12-29 14:59 出处:网络
I have a method as an NSString *. If it exists, I want to call it, and if not, do nothing. SEL eventSelector = NSSelectorFromString(eventSelectorStr);

I have a method as an NSString *. If it exists, I want to call it, and if not, do nothing.

SEL eventSelector = NSSelectorFromString(eventSelectorStr);
if ([delegate respondsToSelector:eventSelector]) {
    [delegate performSelector:eventSelector];
    [delegate adapterDidFinishAdRequest:self];
}
else {
    // Does not implement selector
}

This code does not work, since NSSelectorFromString will register the string as a selector, so respondsToSelector:even开发者_运维技巧tSelector will cause a crash because the selector is actually invalid.


Why do you say that that doesn't work? This is the most common way to implement invoking optional delegate methods. I've never had an issue with that construct not working.


Let's clear up some confusion.

NSSelectorFromString() will generate a valid selector from the string, and will not crash in doing so. respondsToSelector: will validly determine if the delegate implements that method or not, without crashing. It is true that if you call performSelector: with a selector that the delegate doesn't implement it will cause a crash.

However, that isn't the situation here. The code is valid. If you have a crash in this code, I would check the error message and look instead to adapterDidFinishAdRequest:.


Try checking for eventSelector != nil before using it.


It took me a while to understand this as well. The key insights are that a selector reference is basically just a dressed-up C string, and that it doesn't 'belong' to any particular class or object. When the NSSelectorFromString() documentation says that the selector is 'registered', it just means that a dressed-up C string is 'blessed' (my term) for use as a selector within the Objective-C runtime.

Here's the section on the return value for NSSelectorFromString() from Apple's documentation:

Return Value

The selector named by aSelectorName. If aSelectorName is nil, or cannot be converted to UTF-8 (this should be only due to insufficient memory), returns (SEL)0.

Reading it carefully shows that the only situations which can cause (SEL)0 to be returned are if your string was nil or if you ran out of memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消