Ankit0047
Ankit0047

Reputation: 144

swift: How to use protocols in place of abstract class?

I wanted to know how can i use protocols in the place of abstract class.Given below is the scenario written with abstract class that i want to implement using protocols in swift:

public abstract class ModelA
{
  protected int productQuantity;
  protected String productUrl;
}

class ModelB extends ModelA
{
  // I want to access ModelA members here
}

I want to implement the above functionality using protocols in swift. Any help will be appreciated.

Upvotes: 0

Views: 375

Answers (3)

Mikael
Mikael

Reputation: 2395

you can write this:

@objc protocol modelA
{
  @objc optional productQuantity:Int { get set }
  @objc optional productUrl:String { get set }
}

And then use it in a class

class ModelB: ModelA {
  // you can implement OR NOT the attributes here
}

from apple doc:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

You can define optional requirements for protocols, These requirements do not have to be implemented by types that conform to the protocol. Optional requirements are prefixed by the optional modifier as part of the protocol’s definition. Optional requirements are available so that you can write code that interoperates with Objective-C. Both the protocol and the optional requirement must be marked with the @objc attribute. Note that @objc protocols can be adopted only by classes that inherit from Objective-C classes or other @objc classes. They can’t be adopted by structures or enumerations.

But honestly, It's not really the best approach if you want to really keep your code clear. Abstract classes don't define clearly who will implement functions or not. I don't know what you are trying to achieve in the end but maybe extensions could help you add functions to some classes without having to define them in a protocol before hands.

Upvotes: 1

Anni S
Anni S

Reputation: 2026

Here is a simple example and also for more details you can look at Apple Docs

protocol Shape {
    var width: Double { get set}
    var height: Double { get set}
    func area() -> Double
    func perimeter() -> Double
}

class Rectangle:Shape
{
    var width: Double
    var height: Double

    init()
    {
        width = 0.0
        height = 0.0
    }
    convenience init(width:Double, height:Double)
    {
        self.init()
        self.width = width
        self.height = height
    }
    func area() -> Double {
        return width * height
    }

    func perimeter() -> Double {
        return 2 * (width + height)
    }
}

|

    var rect = Rectangle(width: 5.0, height: 5.0)
    print(rect.area())
    print(rect.perimeter())

    rect = Rectangle()
    print(rect.area())
    print(rect.perimeter())

    rect.width = 10
    rect.height = 10
    print(rect.area())
    print(rect.perimeter())

Upvotes: 1

Ahmad F
Ahmad F

Reputation: 31655

The Implementation of the protocol should be:

protocol ModelA {
    // props
    var productQuantity: Int { get set }
    var productUrl:String { get set }
}

"I want to access ModelA members here", actually you HAVE TO implement ModelA properties in ModelB. Otherwise, you will get a compile error saying:

enter image description here

ModelB:

class ModelB: ModelA {
    // default values
    var productQuantity: Int = 0
    var productUrl: String = ""

    // you can remove the default values after implementing 'init':
    init(productQuantity: Int, productUrl: String) {
        self.productQuantity = productQuantity
        self.productUrl = productUrl
    }
}

For more information about protocol, check Apple Documentation.

Upvotes: 1

Related Questions