iosswiftiboutlet

what is the difference between let and var for reference types, when we create an IBOutlet programmatically in ios, swift


we can define and set constraints programmatically like below in swift. I created four label outlets in four different ways. like below

 var labelone : UILabel = {
        var label = UILabel()
        label.text = "Stack"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    let labeltwo : UILabel = {
        let label = UILabel()
        label.text = "Overflow"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()


    var labelthree : UILabel = {
        let label = UILabel()
        label.text = "Confused"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    let labelfour: UILabel = {
        var label = UILabel()
        label.text = "More confused"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()


    func addconstaraints() {
        labelone.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        labelone.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        labelone.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

        labeltwo.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        labeltwo.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        labeltwo.topAnchor.constraint(equalTo: labelone.bottomAnchor).isActive = true

        labelthree.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        labelthree.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        labelthree.topAnchor.constraint(equalTo: labeltwo.bottomAnchor).isActive = true

        labelfour.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        labelfour.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        labelfour.topAnchor.constraint(equalTo: labelthree.bottomAnchor).isActive = true
    }

all are working fine. are there any differences. hope your help to understand this.

I know the difference between let and var for variable define. is it same for the outlets. how can we see the difference in outlet


Solution

  • In your example, you're considering using var vs. let in declaring the property itself, as well as inside the closure that initialized that property. In answer to your question, bottom line, anywhere you can use let (for constants), you theoretically can use var (for variables), but you should only do so if you're planning on changing that variable later. In the case of a reference type, like UILabel, this means if you plan on replacing that label with an entirely new instance of UILabel.

    So, the first and fourth options, where var was used inside the closure, can be dismissed out of hand as poor programming style because you're not changing it again within the scope of the closure, so we know we should use let inside the closure. Regarding second or third options (i.e. whether the property, itself, should be constant or variable), the question is whether you're ever going to replace that UILabel with another, later. If so, you have to use the third option. But we can suspect that this was unlikely to be your intent, and so if you don't plan on replacing that label later, of these four options, you would favor the second option of let/let.


    Having said that, this looks like this is in a view controller, and I wouldn't advise instantiating any view objects during the instantiation of the view controller. Usually that's deferred to viewDidLoad or if the entire view hierarchy is built programmatically, in loadView. Or, even better, we get out of the business of building controls manually and we let the storyboard instantiate IBOutlet references at the appropriate time.