直接上干货
@interface TestClass : NSObject
- (void)func;
- (void)func2: (int) val;
- (NSString*)func3;
- (int)func4:(int)x y:(int)y z:(int)z;
@end
@implementation TestClass
- (void)func {
NSLog(@"func");
}
- (void)func2: (int) val {
NSLog(@"func2: %d", val);
}
- (NSString*)func3 {
return @"func3";
}
- (int)func4:(int)x y:(int)y z:(int)z {
return x + y + z;
}
@end
调用
// 法1
TestClass *p1 = [[TestClass alloc] init];
[p1 func];
[p1 func2: 1];
NSLog(@"%@", [p1 func3]);
NSLog(@"%d", [p1 func4:1 y:2 z:3]);
// 发2
id p2 = ((id (*)(Class, SEL))(void *)objc_msgSend)(objc_getClass("Person"), sel_registerName("alloc"));
((void (*)(id, SEL))objc_msgSend)(p2, sel_registerName("func"));
((void (*)(id, SEL, int))objc_msgSend)(p2, sel_registerName("func2:"), 2);
NSLog(@"%@", ((NSString* (*)(id, SEL))objc_msgSend)(p2, sel_registerName("func3")));
NSLog(@"%d", ((int (*)(id, SEL, int, int, int))objc_msgSend)(p2, sel_registerName("func4:y:z:"), 1, 2, 3));