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