In objective-C, a message processing method called: withobject:

Source: Internet
Author: User

Summary: 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.

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 message passing function isIDObjc_msgsend (IDThereceiver,SelTheselector ,...)

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. BottomCodeLet's take a look at how to generate a sel if a message is transmitted.

First, create a simple function.

-(Void) Foonoinputs {

Nslog (@ "Does nothing");

}

Then call it

[SelfPerformselector:@ 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

[SelfPerformselector:@ Selector(Foooneinput :) withobject:@ "First"];

Third test more parameters

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

Nslog (@ "Logs % @ then % @", First, second );

}

Then call it

[SelfPerformselector:@ 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

SelMytestselector =@ Selector(Mytest :);

And the function we call is in another class.

-(Void) Abcwithaaa :(Nsnumber*) Number {

IntPrimarykey = [numberIntvalue];

Nslog("% I", Primarykey );

}

Methodforselectors* MFs = [Methodforselectors Alloc];

Nsarray* Arrays = [Nsarray Arraywithobjects:@ "AAA",@ "BBB",Nil];

For(Nsstring* ArrayInArrays ){

SelCustomselector =Nsselectorfromstring([NsstringStringwithformat:@ "Abcwith % @:", Array]);

MFs = [[Methodforselectors Alloc]Performselector: CustomselectorWithobject: 0];

}

Complete code:

@ ImplementationClassforselectors
-(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 {

IntPrimarykey = [numberIntvalue];

Nslog("% I", Primarykey );

}

 

-(Void) Specified parameter hodsviaselectors {

[SelfPerformselector:@ Selector(Foonoinputs)];

[SelfPerformselector:@ Selector(Foooneinput :) withobject:@ "First"];

[SelfPerformselector:@ Selector(Foofirstinput: secondinput :) withobject:@ "First"Withobject:@ "Second"];

}

 

-(Void) Export mdynamicmethodsviaselectors {

Methodforselectors* MFs = [Methodforselectors Alloc];

Nsarray* Arrays = [Nsarray Arraywithobjects:@ "AAA",@ "BBB",Nil];

For(Nsstring* ArrayInArrays ){

SelCustomselector =Nsselectorfromstring([NsstringStringwithformat:@ "Abcwith % @:", Array]);

MFs = [[Methodforselectors Alloc]Performselector: CustomselectorWithobject: 0];

}

}

@ End

@ ImplementationMethodforselectors

-(Void) Abcwithaaa :(Nsnumber*) Number {

Nslog("% I", Number );

}

@ End

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.