Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Variables in protocol is possible?

Tags:

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?

like image 793
Renuka Pandey Avatar asked Jun 01 '17 12:06

Renuka Pandey


People also ask

Can we define optional method in Swift protocol?

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.

How do you declare optional in protocol?

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.

How do you make a protocol variable optional in Swift?

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.

What is optional function in Swift?

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.


2 Answers

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 {}     } } 
like image 118
Renuka Pandey Avatar answered Oct 07 '22 19:10

Renuka Pandey


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     } } 
like image 29
Alexandre G. Avatar answered Oct 07 '22 19:10

Alexandre G.