protocol TestProtocol {
var name : String {set get}
var age : Int {set get}
}
Provide the default extension for the protocol. Provide a default implementation for all given variables and get what they want them to be optional.
In the protocol below, name and age are optional.
extension TestProtocol {
var name: String {
get { return "Any default Name"} set {}
}
var age : Int {get{return "23"} set{}}
}
Now, if I follow the above protocol for any other class, for example
class TestViewController: UIViewController, TestProtocol{
var itemName: String = ""
**I can implement the name only, and my objective is achieved here, that the controller will not give a warning that "TestViewController does not conform to protocol TestProtocol"**
var name: String {
get {
return itemName ?? ""
} set {}
}
}
source
share