微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IOS学习笔记十三(使用类别实现非正式协议)

1、协议

可以理解为java里面的接口

 

 

 

 

2、测试Demo

NSObject+chenyu.h

#ifndef NSObject_chenyu_h
#define NSObject_chenyu_h
#import <Foundation/Foundation.h>

@interface NSObject(chenyu)
-(void)test;
@end

#endif /* NSObject_chenyu_h */

 

Org.h

#ifndef Org_h

#define Org_h

#import <Foundation/Foundation.h>

#import "NSObject+chenyu.h"

@interface Org : NSObject

@end



#endif /* Org_h */

 

 

Org.m

#import <Foundation/Foundation.h>
#import "Org.h"
//#import "NSObject+chenyu.h"

@implementation Org
//如果不实现协议中的方法会发生错误
-(void)test
{
    NSLog(@"this is org test method");
}
@end

 

 

 

main.m

#import "Org.h"

int main(int argc,char * argv[]) {
    @autoreleasepool {
        Org *org = [Org new];
        [org test];
    }
}

 

 

 

 

 


3、运行结果

this is org test method

 

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐