Objective-C 协议和运行时检查方法、类是否存在

协议的声明:

 1 //
 2 //  Person.h
 3 //  TestOC01
 4 //
 5 //  Created by xinye on 13-10-23.
 6 //  Copyright (c) 2013年 xinye. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @protocol Person <NSObject>
12 
13 
14 @property (nonatomic,strong) NSString *firstName;
15 @property (nonatomic,strong) NSString *lastName;
16 @property (nonatomic,unsafe_unretained) NSUInteger age;
17 
18 @optional
19 -(id<Person>) initWithFirstName:(NSString *) firstName
20                        lastName:(NSString *) lastName
21                             age:(NSUInteger) age;
22 @required
23 -(id<Person>) initWithNil;
24 @end

实现协议:

 1 //
 2 //  Father.h
 3 //  TestOC01
 4 //
 5 //  Created by xinye on 13-10-23.
 6 //  Copyright (c) 2013年 xinye. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 
12 @interface Father : NSObject <Person>
13 
14 +(void) sayNil;
15 
16 
17 @end
18 
19 
20 //
21 //  Father.m
22 //  TestOC01
23 //
24 //  Created by xinye on 13-10-23.
25 //  Copyright (c) 2013年 xinye. All rights reserved.
26 //
27 
28 #import "Father.h"
29 
30 @implementation Father
31 // 实现一个协议,必须实现其@required标记的方法,并且必须@synthesize协议中定义的@requeired属性,协议中定义的方法和属性默认都是@required的
32 @synthesize firstName,lastName,age;
33 
34 -(id<Person>) initWithFirstName:(NSString *)_firstName lastName:(NSString *)_lastName age:(NSUInteger)_age
35 {
36     self = [super init];
37     if (self) {
38         self.firstName = _firstName;
39         self.lastName = _lastName;
40         self.age = _age;
41     }
42     
43     return self;
44 }
45 
46 -(id<Person>) initWithNil
47 {
48     self = [super init];
49     return self;
50 }
51 
52 +(void) sayNil
53 {
54     NSLog(@"say Nil Method");
55 }
56 @end

测试:

 1 //
 2 //  main.m
 3 //  TestOC01
 4 //
 5 //  Created by xinye on 13-10-23.
 6 //  Copyright (c) 2013年 xinye. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 #import "Father.h"
12 
13 
14 int main(int argc, const char * argv[])
15 {
16 
17     @autoreleasepool {
18         
19         id<Person> per = [[Father alloc]initWithFirstName:@"张" lastName:@"三" age:111];
20         
21         NSLog(@"姓名:%@",[[per firstName] stringByAppendingString:per.lastName]);
22         NSLog(@"年龄:%li",per.age);
23         // 检测是否有实例方法
24         if([Father instancesRespondToSelector:@selector(initWithNil)]){
25             NSLog(@"*****Father 类中有一个实例方法:initWithNil");
26         }else{
27             NSLog(@"Father 类中没有initWithNil实例方法");
28         }
29         
30         // 检测是否有类方法
31         if([Father respondsToSelector:@selector(sayNil)]){
32             NSLog(@"*****Father 类中有sayNil类方法");
33         }else{
34             NSLog(@"Father 类中没有sayNil类方法");
35         }
36         
37         // 检测是否有实例方法
38         if([per respondsToSelector:@selector(initWithFirstName:lastName:age:)]){
39             NSLog(@"*****Father 类中有initWithFirstName:lastName:age:实例方法");
40         }else{
41             NSLog(@"Father 类中没有initWithFirstName:lastName:age:实例方法");
42         }
43         
44         // 检测指定的类是否存在
45         if(NSClassFromString(@"NSString") != nil){
46             NSLog(@"=========当前版本中存在NSString类");
47         }else{
48             NSLog(@"$$$$$$$$$当前版本中不存在NSString类");
49         }
50         if(NSClassFromString(@"NBString") != nil){
51             NSLog(@"=========当前版本中存在NBString类");
52         }else{
53             NSLog(@"$$$$$$$$$当前版本中不存在NBString类");
54         }
55         
56     }
57     return 0;
58 }