Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift making IBOutlet as strong

Tags:

IBOutlets are weak by default in Swift. I have an object in the viewController Created in the storyboard which is not in the view hierarchy , So I need it to be a strong reference in ViewController , How can I Change @IBoutlet property to strong.

like image 689
Yatheesha Avatar asked Jun 24 '14 04:06

Yatheesha


People also ask

Should IBOutlet be strong or weak?

The official answer from Apple is that IBOutlets should be strong. The only case when an IBOutlet should be weak is to avoid a retain cycle. A strong reference cycle can result in memory leaks and app crashes.

Why is IBOutlet weak in Swift?

Outlets that you create will therefore typically be weak by default, because: Outlets that you create to, for example, subviews of a view controller's view or a window controller's window, are arbitrary references between objects that do not imply ownership.

What is the difference between IBOutlet and IBAction?

An IBOutlet is for hooking up a property to a view when designing your XIB. An IBAction is for hooking a method (action) up to a view when designing your XIB. An IBOutlet lets you reference the view from your controller code.

What does IBOutlet mean in Swift?

The type qualifier IBOutlet is a tag applied to an property declaration so that the Interface Builder application can recognize the property as an outlet and synchronize the display and connection of it with Xcode. An outlet is declared as a weak reference ( weak ) to prevent strong reference cycles.


2 Answers

You can make an IBOutlet strong by either selecting strong when connecting the outlet: enter image description here

Or just remove the weak keyword from the declaration:

@IBOutlet var label: UILabel! 
like image 68
Adam Johns Avatar answered Sep 20 '22 08:09

Adam Johns


Change the name of the outlet, it might be a reserved name so you are trying to override it.

like image 31
Dalal Alharbi Avatar answered Sep 18 '22 08:09

Dalal Alharbi