Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

Declaring required properties

Not surprisingly, we can also add properties to a protocol. Protocol properties must be declared with var, and can be declared to be settable (that is, read--only) or settable and gettable (meaning read and write).

Let's add a couple of properties to the TalkativePerson protocol:

protocol TalkativePerson: Talkative 
{
var name: String { get }
var address: String { getset}
}

TalkativePerson contains everything from talkative, and adds two further properties. We assume (for the sake of the demonstration) that people don't change their names, but do change their addresses.

Using { get set } will prevent types that conform to the TalkativePerson protocol from declaring their address property with let.