Swift, two issues. 1) weak var 2) bang operator for @IBOutlet

shle2821 picture shle2821 · Mar 12, 2015 · Viewed 8.1k times · Source

Per:

@IBOutlet weak var nameLabel: UILabel!
  1. Whenever I declare my IBOutlets, i just use var instead of weak var. But I've recently come across a few code templates that use weak var. Why do they do it? What's the added benefit?

  2. Why is there a bang operator at the end of UILabel. I know it's required and i go along w/ it, but just asking it now.

Thanks in advance.

Answer

Jérôme picture Jérôme · Mar 12, 2015
  1. Swift IBOutlet are weak by default (but others properties are strong by default). So both writing are the same.

You have more details about the difference between weak and strong here

  1. According to apple documentation

When you declare an outlet in Swift, you should make the type of the outlet an implicitly unwrapped optional (!). This way, you can let the storyboard connect the outlets at runtime, after initialization.