PerformSelector may cause a leak because its selector is unknown Solution

Source: Internet
Author: User

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 ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.