An iOS implementation method that calls a dynamic method name has a return value

Source: Internet
Author: User

Demand:

There are 10 classes, each of which has n methods (provided the method names are regularly followed, such as seta0,seta1 ...) If you go to the Init class, and then call the method one at a-so you don't have to do anything else in a day

A simple workaround is to refer to this: The dynamic invocation of classes and methods in iphone development

Although Performselector has a return value, there is a warning under ARC because the method name is dynamic and the system does not know what type the return value is. The solution can be found in this article: "Performselector may cause a leak because it selector is unknown" warning causes and solutions

Full text reproduced here:

Problem description

A project was used to create a selector from a string, and a compile-time discovery Warning: "Performselector may cause a leak because it selector is unknown" Why does this warning appear in ARC mode because the Performselector selector is unknown and may cause a leak?

After searching, a satisfying answer was found on the StackOverflow. See Http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown.

Reason

In Arc mode, the runtime needs to know how to handle the return value of the method you are calling. This return value can be any value, such as,,, void int , and char NSString id so on. ARC obtains this information through the function definition of the header file. So the static selector that we use usually doesn't show this warning. Because this information has been determined during compilation.

Such as:

... [Somecontroller performselector: @selector (somemethod)];...-(void) somemethod{//bla bla ...}

And [someController performSelector: NSSelectorFromString(@"someMethod")]; when used, ARC doesn't know what the return value of the method is, and what to do with it? The Ignore? Or is it marked ns_returns_retained or not ns_returns_autoreleased ?

Solutions1. Using the function pointer method
SEL selector = nsselectorfromstring (@ "SomeMethod"); Imp imp = [_controller methodforselector:selector];void (*FUNC) (ID, SEL) = (void *) Imp;func (_controller, selector);

When there are additional parameters, such as

SEL selector = nsselectorfromstring (@ "Processregion:ofview:"); imp imp = [_controller methodforselector:selector]; CGRect (*FUNC) (ID, SEL, cgrect, UIView *) = (void *) imp; CGRect result = func (_controller, selector, somerect, Someview);
2. Use macros to ignore warnings
#pragma clang diagnostic push #pragma clang diagnostic ignored "-warc-performselector-leaks" [Somecontroller Performsel Ector:nsselectorfromstring (@ "SomeMethod")] #pragma clang diagnostic pop

By using #pragma clang diagnostic push/pop , you can tell the clang compiler to ignore specific warnings only for a specific part of the code.

If there are multiple warnings to ignore, you can define a macro

#define SUPPRESSPERFORMSELECTORLEAKWARNING (Stuff) \ Do {\ _pragma ("Clang diagnostic push") \ _pragma ("cl Ang diagnostic ignored \ "-warc-performselector-leaks\" ") Stuff; \ _pragma ("clang diagnostic Pop")} while (0)

performSelectoruse the macro where the warning is generated, such as

Suppressperformselectorleakwarning ([_target performselector:_action withobject:self]);

If you need to performSelector return a value,

ID result; Suppressperformselectorleakwarning (result = [_target performselector:_action withobject:self]);
3. Using Afterdelay
[Self performselector:aselector withobject:nil afterdelay:0.0];

You can do this if you are allowed to execute in the next Runloop within the accepted range. XCode5 no problem, but it is reflected that xCode6 words this can not eliminate the warning.


An iOS implementation method that calls a dynamic method name has a return value

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.