how to create a method with a selector parameter

objective-c, parameter-passing, selector

Solution

EDIT 1:

Your error is here:

 [self performSelectorOnMainThread:@selector(selector)
 ----------------------------------^^^^^^^^^^^^^^^^^^^

`selector` is already a selector, and your are putting it in `@selector(`) again.

It should be

 [self performSelectorOnMainThread:selector withObject:data waitUntilDone:YES];

Edit 0:

Your codes are correct. And you get the error only when your SEL method in not found in the current class.

As in the code below, if I change the method name `sum` to `add` this will throw the same error.

-(void)sum{
    NSLog(@"sum");
}

-(void) createURL: (SEL) selector{
    [self performSelector:selector];
}

- (IBAction)total:(id)sender {
    SEL sel = NSSelectorFromString(@"sum");
    [self createURL:sel];
}

Also, you need to meet the method signature as well. If your method takes one argument, You need to send exactly one.

-(void)sum:(id)integer{
    NSLog(@"sum:%ld",[integer integerValue]);
}

-(void) createURL: (SEL) selector{
    //NSData* data = [NSData dataWithContentsOfURL: nil];
    //[self performSelectorOnMainThread:@selector(selector) withObject:data waitUntilDone:YES];

    [self performSelector:selector withObject:@(12)];
}

- (IBAction)total:(id)sender {
    NSInteger total=0;

    for (id element in self.arrayController.arrangedObjects) {
        total += [element firstNumber]*[element secondNumber];
    }
    [self.label setStringValue:[NSString stringWithFormat:@"%ld",total]];


    SEL sel = NSSelectorFromString(@"sum:");
    [self createURL:sel];

}

Problem

Hi I'm trying to make an objective-C method that has a selector as parameter. But I keep receiving the "unrecognized selector sent to instance " error message. I'm doing the following: this is the method with the selector parameter: ``` -(void)createURL: (SEL) selector{ dispatch_sync(kBgQueue,^{ NSData* data = [NSData dataWithContentsOfURL: wcfURL]; [self performSelectorOnMainThread:@selector(selector) withObject:data waitUntilDone:YES]; }); } ``` this is the method i want to be the selector: ``` -(void)fetchedUserType:(NSData *)responseData{ NSError* error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; userType = [json objectForKey:@"getUserTypeResult"]; } ``` and this is where I'm calling my method: ``` - (void)viewDidLoad { [super viewDidLoad]; NSString *url = [NSString stringWithFormat:@"http://10.211.55.3:1234/Service1.svc/getUserType/%@", userEmail]; wcfURL = [[NSURL alloc]initWithString:url]; SEL sel = NSSelectorFromString(@"fetchedUserType:"); [self createURL:sel]; ``` }

Original source