What does the underscore character "_" mean before the quick variable is specified?

Here is the code:

var _selected: Bool = false {
    didSet(selected) {
        let animation = CATransition()
        animation.type = kCATransitionFade
        animation.duration = 0.15
        self.label.layer.addAnimation(animation, forKey: "")
        self.label.font = self.selected ? self.highlightedFont : self.font
    }
}

Why is the variable "_selected" instead of "selected"?

+4
source share
1 answer

This is just a coding style that should not be applied to Swift code.

Developers often mark things with underlines to indicate that they should be private.

However, a practical application for underlining _. Read more about Local and external parameter names for methods .


So how do you avoid using _selected? Right now, you have two variables, when you already have the one you need (selected).

Deleting _ will require you to override the member variable (how to do this).

override var selected: Bool {
    didSet {
        println("Hello, \(selected)")
    }
}

, setSelected (: ), .

+7

All Articles