简体   繁体   中英

iOS what does performSelector:withObject:withObject: really do?

What's different between selectors with same arguments type but different arguments sort?

I got these two selectors with same arguments type.

- (void)methodWithCallBack:(void(^)(void)) cb double:(double)value  {
  NSLog(@"%s %f", __PRETTY_FUNCTION__, value);
  if (cb) {
      cb();
  }
}

- (void)methodWithDouble:(double)value callBack:(void(^)(void)) cb  {
  NSLog(@"%s %f", __PRETTY_FUNCTION__, value);
  if (cb) {
      cb();
  }
}

But when performSelector:withObject:withObject: called with these selectors, I got different result.

[self performSelector:@selector(methodWithDouble:callBack:) withObject:@(2.5) withObject:[^(void){
    NSLog(@"Test Call Back Double");
} copy]];

[self performSelector:@selector(methodWithCallBack:double:) withObject:[^(void){
    NSLog(@"Test Call Back Double");
} copy] withObject:@(2.5)];

How does this happend? What does performSelector:withObject:withObject: really do?

I have no idea what @(2.5) is. NSNumber literal is @2.5. a double is non object and NSNumber is object. You should pass an NSNumber .

On a second note: I feel weird that Apple created performSelector:withObject:withObject: . A single " withObject " is suffice actually, you just pass an NSArray with objects you want to pass to that.

Eg.

NSNumber *var1 = @2.5;
NSString *yourMom = @"Sally";
NSArray *params = [NSArray arrayWithObjects:var,yourMom,nil];
[self performSelector:@selector(goMethod:) withObject:params];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM