iOS如何实现协议的@property [英] iOS how to implement a protocol's @property
问题描述
我的理解是协议指定方法名称,并且符合该协议的其他人执行方法。
What I understand is that a protocol specify method names, and someone else who conforms to that protocol do the implementation of the methods.
那么在协议中声明的属性呢?是实现一个属性意味着实现它的setter和getter?
So what about the properties declared in a protocol? Is that to implement a property means to implement its setter and getter?
推荐答案
属性是一个或两个具有特定签名的方法的奇特名称,为此Objective-C提供一个约定,他们使用替代点。
语法。一个协议声明,例如一对
Property is a fancy name for one or two methods with specific signatures for which Objective-C provides a convention that lets you call them using the alternative dot .
syntax. There is no difference between a protocol declaring, say, a pair of
-(int) foo;
-(void)setFoo:(int)_foo;
方法和协议声明读写属性:
methods, and a protocol declaring a read-write property:
@property (readwrite) foo;
所以你是绝对正确的,实现一个属性意味着实现一个或两个方法,只读,只写或读写属性。
So you are absolutely right, implementing a property means implementing one or two methods, depending on whether you implement a read-only, write-only, or a read-write property.
这篇关于iOS如何实现协议的@property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!