objective-c 笔记6: 协议
1.声明协议
关键字:@protocol
eg:
@protocol NSCoping //协议名称 -(id) copyWithZone : (NSZone *) zone; @end
2. 使用协议
@interface Car : NSObject<NSCoping,NSCoding> { //instance variables } // methods @end //Car
可以实现多个协议,NSCoding 声明省略。
Objective-C 2.0增加了两个新的协议修饰符:@optionnal和@required,默认是@required
@protocol MyProtocol - (void)requiredMethod; @optional - (void)anOptionalMethod; - (void)anotherOptionalMethod; @required - (void)anotherRequiredMethod; @end//MyProtocol
Objective-C通过类遵循的协议和类继承的超类来定义类的类型。您可以通过发送conformsToProtocol:消息来检查一个类是否遵循特定的协议:
if ([anObject conformsToProtocol:@protocol(MyProtocol)]){
// do something appropriate
}
相关推荐
fort0 2020-05-16
81570790 2020-04-16
86540698 2020-04-08
zhoutaifeng 2020-03-07
好好学习天天 2020-03-06
InterestSoul 2020-02-17
82467413 2019-12-31
84407518 2012-07-16
81570790 2013-07-01
83254851 2013-08-29
84334052 2019-11-19
86540698 2019-11-19
84467715 2011-07-25
81731290 2014-03-22
85437811 2014-03-22
dahuichen 2019-10-25
ObjectiveC 2011-09-20
80530297 2015-04-14
80530297 2015-04-20