官术网_书友最值得收藏!

How it works...

Protocols are defined with the protocol keyword, and the implementation is contained within curly brackets. It is conventional to begin a protocol name with a capital letter, and to name a protocol as either something that the type is or something that it does; in this protocol, we are declaring that any type implementing it is saveable.

Types implementing this protocol have two parts of the interface to implement.

The Saveable protocol declares that anything implementing it needs to have a variable called saveNeeded, which is a Bool. This property will indicate that the information held in the remote database is out of date and a save is needed. In addition to the usual property declaration, a protocol requires us to define whether the property can be accessed (get) and changed (set), which is added in curly brackets after the type declaration. Removing the set keywords makes it a read-only variable. Consider the given example:

var saveNeeded: Bool { get set } 
Defining a protocol property as read-only doesn't prevent an implementing type from allowing the property to be set, just that the setting of that property isn't defined in the interface.

The second part of our protocol definition is to describe the method we can call to save the information to the remote database. This func declaration is exactly the same as other function declarations we have seen; however, the implementation of the function, usually contained in the curly brackets, is omitted as this is provided by the implementing type:

func saveToRemoteDatabase(handler: @escaping (Bool) -> Void) 

Now that we have defined our protocol, we need to implement the Saveable protocol on our Person class that we have been using throughout this chapter:

class Person: Saveable { 
//....
var saveHandler: ((Bool) -> Void)?

func saveToRemoteDatabase(handler: @escaping (Bool) -> Void) {
saveHandler = handler
// Send person information to remove database
// Once remote save is complete, it calls saveComplete(Bool)
}

func saveComplete(success: Bool) {
saveHandler?(success)
}
}

We conform to a protocol, and in a similar way, we declare that an object inherits from another object, by adding the protocol name after the type name, separated by :. By adding this conformance, the compiler will complain that our Person object doesn't implement part of the protocol as we haven't declared a saveNeeded property, so let's add that:

class Person: Saveable { 
//....
var saveHandler: ((Bool) -> Void)?
var saveNeeded: Bool = true

func saveToRemoteDatabase(handler: @escaping (Bool) -> Void) {
saveHandler = handler
// Send person information to remove database
// Once remote save is complete, it calls saveComplete(Bool)
}

func saveComplete(success: Bool) {
saveHandler?(success)
}
}

We'll add a default value of true since when an instance of this object is created, it won't be in the remote database, and so it will need saving.

主站蜘蛛池模板: 庐江县| 中卫市| 朝阳区| 丹棱县| 安多县| 沧州市| 汉源县| 通榆县| 汽车| 恩施市| 新巴尔虎左旗| 宜丰县| 任丘市| 东兰县| 龙泉市| 仲巴县| 龙岩市| 金阳县| 宣武区| 长宁区| 石河子市| 龙南县| 得荣县| 黄平县| 彭水| 孙吴县| 六安市| 南昌市| 惠东县| 南木林县| 绥化市| 历史| 晋州市| 张家港市| 新蔡县| 砚山县| 剑川县| 恩平市| 渑池县| 社旗县| 开封县|