A Message Processing Method in objective-C called performselector

Source: Internet
Author: User

In objective-C, the method used to call a function is "message transmission". The difference between this method and a common function call is that you can transmit any message to an object at any time, instead of declaring these methods during compilation. Therefore, objective-C can deliver people and messages at runtime.

 

First, we will introduce two methods: SEL and @ selector.

According to the official appleobjective-C Runtime reference documentation, the function for passing messages is ID objc_msgsend (ID thereceiver, Sel theselector ,...)

Thereceiver indicates that the type of the message receiving object is ID, and the theselector indicates that the type of the message name is Sel. The following code shows how to generate a sel if a message is transmitted.

First, create a simple function.

-(Void) foonoinputs {

Nslog (@ "does nothing ");

}

Then call it

[Self defined mselector: @ selector (foonoinputs)];

The second test shows how to transmit parameters in a message.

We create a function with input parameters.

-(Void) foooneiput :( nsstring *) first {

Nslog (@ "logs % @", first );

}

Then call it

[Self defined mselector: @ selector (foooneinput :) withobject: @ "first"];

Third test more parameters

-(Void) foofirstinput :( nsstring *) First secondinput :( nsstring *) Second {

Nslog (@ "logs % @ then % @", first, second );

}

Then call it

[Self defined mselector: @ selector (foofirstinput: secondinput :) withobject: @ "first" withobject: @ "second"];

In the fourth experiment, how to create dynamic functions and then call them? We need to create a selector

Sel mytestselector = @ selector (mytest :);

And the function we call is in another class.

-(Void) abcwithaaa: (nsnumber *) number {

Int primarykey = [number intvalue];

Nslog ("% I", primarykey );

}

Methodforselectors * MFs = [[methodforselectors alloc] init];

Nsarray * arrays = [nsarray arraywithobjects: @ "AAA", @ "BBB", nil];

For (nsstring * array in arrays ){

Sel customselector = nsselectorfromstring ([nsstringstringwithformat: @ "abcwith % @:", array]);

MFs = [[methodforselectors alloc] performselector: customselector withobject: 0];

}

Note: updated at 20120606

1. If arc is used, a warning "Your mselector may cause a leak because its selector is unknown" will be generated.
2. This method will continue to run when a message that does not conform to the Conventions is passed in. No error is reported. For example, two parameters should be input, but only one parameter should be input. Or three parameters are passed in. The third parameter is not initialized.

There is another method to call other class functions, but there is no parameter. We suppose there is no parameter here, so we can do this.

[MFs customselector];

Complete code:

@ Implementation classforselectors
-(Void) foonoinputs {

Nslog (@ "does nothing ");

}

-(Void) foooneiput :( nsstring *) first {

Nslog (@ "logs % @", first );

}

-(Void) foofirstinput :( nsstring *) First secondinput :( nsstring *) Second {

Nslog (@ "logs % @ then % @", first, second );

}

 

-(Nsarray *) abcwithaaa: (nsnumber *) number {

Int primarykey = [number intvalue];

Nslog ("% I", primarykey );

}

 

-(Void) implements callback hodsviaselectors {

[Self defined mselector: @ selector (foonoinputs)];

[Self defined mselector: @ selector (foooneinput :) withobject: @ "first"];

[Self defined mselector: @ selector (foofirstinput: secondinput :) withobject: @ "first" withobject: @ "second"];

}

 

-(Void) specify mdynamicmethodsviaselectors {

Methodforselectors * MFs = [methodforselectors alloc];

Nsarray * arrays = [nsarray arraywithobjects: @ "AAA", @ "BBB", nil];

For (nsstring * array in arrays ){

Sel customselector = nsselectorfromstring ([nsstringstringwithformat: @ "abcwith % @:", array]);

MFs = [[methodforselectors alloc] performselector: customselector withobject: 0];

}

}

@ End

 
@ Implementation methodforselectors

-(Void) abcwithaaa: (nsnumber *) number {

Nslog ("% I", number );

}

@ End

-- EOF --

A Message Processing Method in objective-C called performselector

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.