My technical blog is often maliciously crawled and reproduced by rogue websites. Please move to the original article: Workshop.
When calling a function using the function name or function pointer in Objective-C, the callback function is used as an example. The object is (id) target, and its member function is called callback, I used to write this as follows:
If ([target respondsToSelector: callback]) {[target callback mselector: callback withObject: nil];}
However, a warning message "warning mselector may cause a leak because its selector is unknown" will be reported under ARC.
On the Internet, many people say that the method is to define macros to go to ignore warning and so on. I always feel uncomfortable. The correct solution is found today:
[Solution]
If ([target respondsToSelector: callback]) {// [target callback mselector: callback withObject: nil]; IMP imp = [target methodForSelector: callback]; void (* func) (id, SEL) = (void *) imp; func (target, callback );}
In this way, no warning will be reported.
[Reason]
A detailed explanation shows the adopted answer on the stack.
If you have no translation time, post a link here ~