• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

协议的声明:

 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 }

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
objective-cruntime开发详情发布时间:2022-07-18
下一篇:
IOS开发(objective-c)~开篇有理发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap