创建一个IOSApp类
IOSApp.h文件
1 #import2 3 @interface IOSApp : NSObject 4 5 // 1.添加一个无参数的方法 6 -(void)printInfomation; 7 8 // 2.添加一个有参数的方法 9 -(void)buyApp:(id)appName;10 11 @end
IOSApp.m文件
1 #import "IOSApp.h" 2 3 @implementation IOSApp 4 5 // 3.实现头文件中无参数的方法 6 -(void)printInfomation 7 { 8 NSLog(@"Xcode Interactive Tutorials"); 9 }10 11 // 4.实现头文件中带有参数的方法12 -(void)buyApp:(id)appName13 {14 NSLog(@"Buy the App%@",appName);15 }16 17 @end
ViewController.m 文件
1 #import "ViewController.h" 2 // 5.导入钢材创建的类的头文件 3 #import "IOSApp.h" 4 5 6 @interface ViewController () 7 8 @end 9 10 @implementation ViewController11 12 13 - (void)viewDidLoad {14 [super viewDidLoad];15 // Do any additional setup after loading the view, typically from a nib.16 17 // 6.初始化一个类对象18 IOSApp *app = [[IOSApp alloc] init];19 // 7.@selector()可以理解为取类方法的编号,它的行为基本可以等同c语言中的函数指针,它的结果是SEL类型。20 SEL method = @selector(printInfomation);21 // 8.respondsToSelector()方法,用来判断是否有,以某个名字命名的方法。22 if ([app respondsToSelector:method]){23 24 // 9.performSelector是由运行时系统负责去找方法的,在编译时不做任何校验25 // 调用方法26 [app performSelector:method];27 }28 29 SEL method2 = @selector(buyApp:);30 if ([app respondsToSelector:method2]) {31 // 调用方法32 [app performSelector:method2 withObject:(@"Photoshop Interactive Tutorials")];33 }34 }35 36 37 38 - (void)didReceiveMemoryWarning {39 [super didReceiveMemoryWarning];40 // Dispose of any resources that can be recreated.41 }42 43 @end