A message processing method in the "Go" objective-c performselector:withobject:

Source: Internet
Author: User

The method of calling a function in Objective-c is "message passing", and the difference between this and a normal function call is that you can pass any message to an object at any time without having to declare the methods at compile time. So objective-c can pass people and messages at runtime.

Two methods Sel and @selector are introduced first

According to APPLEOBJECTIVE-C Runtime Reference Official document This message is a function of ID objc_msgsend (ID thereceiver, SEL theselector, ...)

Thereceiver is the object type that accepts the message is Id,theselector is the message name type is sel. The code below let's look at how to generate a sel if the message is delivered.

First, create a simple function

-(void) foonoinputs {

NSLog (@ "Does nothing");

}

And then call it

[Selfperformselector:@selector (foonoinputs)];

Second experiment to see how to pass parameters in a message

We create a function with the input parameter

-(void) Foooneiput: (nsstring*) First {

NSLog (@ "Logs%@", first);

}

And then call it

[Selfperformselector:@selector (foooneinput:) Withobject:@ "First"];

The third experiment more parameters

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

NSLog (@ "Logs%@ then%@", first, second);

}

And then call it

[Selfperformselector:@selector (foofirstinput:secondinput:) Withobject:@ "First" Withobject:@ " Second "];

The fourth experiment how to build a dynamic function and then call them? We need to build a selector

SEL mytestselector = @selector (myTest:);

And the function we call is within 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, it will produce "performselector may cause a leak because it selector is unknown" warning
2. This mode will continue to run without error when passing in a message that does not conform to the contract. For example, 2 parameters should be passed in, but only 1 parameters should be passed in. or 3 parameters are passed in, the third parameter is not initialized.

There is another way to call other class function, but there is no parameter, we assume there are no parameters, then we can

[MFS Customselector];

The 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) performmethodsviaselectors {

[Selfperformselector:@selector (foonoinputs)];

[Selfperformselector:@selector (foooneinput:) Withobject:@ "First"];

[Selfperformselector:@selector (foofirstinput:secondinput:) Withobject:@ "First" Withobject:@ " Second "];

}

-(void) performdynamicmethodsviaselectors {

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

Original: http://www.cnblogs.com/buro79xxd/archive/2012/04/10/2440074.html
@implementation Methodforselectors

-(void) abcwithaaa: (NSNumber *) Number {

NSLog ("%i", number);

}

@end

A message processing method in the "Go" objective-c performselector:withobject:

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.