Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure UIView in viewDidLoad or var's didSet

Tags:

ios

swift

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?

like image 401
Steve Kuo Avatar asked Oct 23 '15 22:10

Steve Kuo


1 Answers

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.

like image 79
rmaddy Avatar answered Sep 25 '22 10:09

rmaddy