What will you do if you weren't afraid ?

You should know the easy-to-use objc_msgSend

    iOS     objc_msgSend, runtime

Too many arguments to function call, expected 0, have 2

When I created an new iOS project, I got this error as shown below.

But why I got this error? It’s easy to know because of Enabling Strict Checking of objc_msgSend Calls in default mode.


To solve it

Select your project -> select your target -> Build Settings -> Search by keyword “objc_msgSend”, only one item be shown -> toggle it to NO


To make use of objc_msgSend for message forwarding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#import "ViewController.h"

#import <objc/message.h>

@interface ViewController ()

@end

@implementation ViewController

//1.无参无返回值
- (void)run1 {
NSLog(@"1.无参无返回值");
}

//2.有参无返回值
- (void)run2:(NSArray *)parameters {
NSLog(@"2.有参无返回值 name=%@ age=%d", parameters[0], [parameters[1] intValue]);
}

//3.无参有返回值
- (NSString *)run3 {
return @"无参有返回值";
}

//4.有参有返回值
- (int)run4:(NSString *)name age:(int)age height:(float)height {
NSLog(@"4.有参有返回值 name=%@, age=%d, height=%lf", name, age, height);
return age + height;
}

- (void)viewDidLoad {
[super viewDidLoad];

//第一种方式,需要关闭objc_msgSend严格检查,步骤: Build Settings -> 搜索“objc_msgsend” -> 设置为NO

//1.无参无返回值
objc_msgSend(self, @selector(run1));

//2.有参无返回值
objc_msgSend(self, @selector(run2:), @[ @"asdf", @12 ]);

//3.无参有返回值
id returnVal3 = objc_msgSend(self, @selector(run3));
NSLog(@"3.收到参数: %@", returnVal3);

//4.有参有返回值
int returnVal4 = (int)objc_msgSend(self, @selector(run4:age:height:), @"qwerty", @22, @60.8);
NSLog(@"收到参数 %d", returnVal4);


NSLog(@"-----------------------------------------------------------");

//第二种方式,不需要关闭objc_msgSend严格检查

//1.无参无返回值
((void (*)(id, SEL))objc_msgSend)(self, NSSelectorFromString(@"run1"));

//2.有参无返回值
((void (*)(id, SEL, id))objc_msgSend)(self, NSSelectorFromString(@"run2:"), @[ @"asdf", @12 ]);

//3.无参有返回值
((id (*)(id, SEL))objc_msgSend)(self, NSSelectorFromString(@"run3"));

//4.有参有返回值
((id (*)(id, SEL, id, id, id))objc_msgSend)(self, NSSelectorFromString(@"run4:age:height:"), @"qwerty", @22, @60.8);

}

@end

Easy to use, doesn’t it? 😊

page PV:  ・  site PV:  ・  site UV: