protocol AProtocol: BProtocol { /// content to be shown on disclaimer Label of cell var disclaimer: String {get set} var cellDisclaimerAttributed: NSAttributedString {get} var showSelection: Bool {get set} var isReadMore: Bool {get} }
I want to make variables optional so that I need not implement all variables every time after conforming protocol. Like in Objective-C we did for methods:
protocol AProtocol: BProtocol { /// content to be shown on disclaimer Label of cell optional var disclaimer: String {get set} optional var cellDisclaimerAttributed: NSAttributedString {get} optional var showSelection: Bool {get set} optional var isReadMore: Bool {get} }
Is it possible?
Swift protocols on their side do not allow optional methods. But if you are making an app for macOS, iOS, tvOS or watchOS you can add the @objc keyword at the beginning of the implementation of your protocol and add @objc follow by optional keyword before each methods you want to be optional.
Put the @optional in front of methods or properties. Compiler Error: 'optional' attribute can only be applied to members of an @objc protocol. @optional isn't even the correct keyword. It's optional , and you must declare the class and protocol wit the @objc attribute.
2 Answers. Show activity on this post. Provide a default extension for the protocol. Provide the default implementation for all the variables set and get which u want them to be optional.
Optional is the mechanism in Swift to indicate the possible absence of a value or a reference to an object. While it can take a bit of getting used to for those unfamiliar with this concept, appropriate use of optionals can make Swift code safer and more expressive.
protocol TestProtocol { var name : String {set get} var age : Int {set get} }
Provide a default extension for the protocol. Provide the default implementation for all the variables set and get which u want them to be optional.
In below protocol, 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 am conforming above protocol to any other class, like
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 {} } }
If you want to conform to Swift's documentation, you'd have to implement it like this :
@objc protocol Named { // variables var name: String { get } @objc optional var age: Int { get } // methods func addTen(to number: Int) -> Int @objc optional func addTwenty(to number: Int) -> Int } class Person: Named { var name: String init(name: String) { self.name = name } func addTen(to number: Int) -> Int { return number + 10 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With