Steve Kuo
Steve Kuo

Reputation: 63064

Configure UIView in viewDidLoad or var's didSet

Consider this example of configuring MKMapView's map type. Should it be done in viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.mapType = MKMapType.Hybrid
}

or in the var's didSet?

@IBOutlet weak var mapView: MKMapView! {
    didSet {
        mapView.mapType = MKMapType.Hybrid
    }
}

Both work, what's the Swift preferred way?

Upvotes: 5

Views: 1040

Answers (1)

rmaddy
rmaddy

Reputation: 318794

They each have a different use.

If you want the mapType set every time the property is set, use didSet.

If you only want the mapType set once when the view is loaded, use viewDidLoad.

Given what you are doing I would say that didSet is the more correct choice here.

BTW - this has nothing to do with "the Swift preferred way". The same logic applies regardless of language.

Upvotes: 8

Related Questions