Search code examples
swiftswift-protocolscomputed-properties

How to define variable that can be set and get in extension of protocol


I come from Java world. Now I am programming in Swift 4.

I would like to implement abstract class in Swift, I know in Swift there is no such concept of abstract class. But I know we could mimic this concept in Swift by using protocol. For example this is what I tried:

// With protocol, I can define functions that concrete class have to implement
protocol ProductProvider {
   func getProductNumber() -> Int
}

// with protocol extension, I can define shared (computed) properties and functions among concrete classes that comply with this protocol
extension ProductProvider {
  var maxProductCount: Int {
        return 10
  }
}

But now, I would like to have a shared variable that could be set & get ("shared" means to be shared with classes that comply with this protocol):

extension ProductProvider {
  var maxProductCount: Int {
        set(newValue) {
           // set to what if I couldn't define private stored variable in extension to hold the most recent set value ?
        }
        get{
           // how to return the most recent value set?
        }
  }
}

My question is in the comment of above code. How can I do that set and get for a variable in extension of protocol in Swift 4? If it is impossible, what are the workarounds possible?


Solution

  • The simplest way i think is define the variable in the protocol with the getter and setter. Then in your conform object you should declare the variable to conformance. An Example:

    protocol AbstractObject {
    
        var maxProductCount: Int { get set }
    }
    
    struct ConformObject: AbstractObject {
    
        var maxProductCount: Int
    }
    

    So now you can use your variable in your default implementations

    extension AbstractObject {
    
        mutating func addOne() -> Int {
            self.maxProductCount += 1
            return self.maxProductCount
        }
    }