Swift - how can I add an extension to an existing class with new properties?

I want to subclass GMSMarker. It is initialized with one property position: CLLocationCoordinate2D. It also has properties that can be set as follows:

let position = CLLocationCoordinate2DMake(51.5, -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.icon = UIImage(named: "house")
london.map = mapView

I want to add a new property rating

I tried like this

class Marker: GMSMarker {

var rating:Int

init(rating: Int){
    self.rating = rating
    super.init()
}

}

But this does not allow you to assign a value to a property ratingusing dot notation.

How can i do this?

+4
source share
1 answer

Change it as follows:

    var rating : Int = 0

convenience init(rating: Int){
    self.init()
    self.rating = 2

}

Swift requires all properties to be fully initialized prior to instantiation.

, , , .

0

Source: https://habr.com/ru/post/1616370/


All Articles