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.
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.
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.
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.
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.
You can make an IBOutlet
strong
by either selecting strong
when connecting the outlet:
Or just remove the weak
keyword from the declaration:
@IBOutlet var label: UILabel!
Change the name of the outlet, it might be a reserved name so you are trying to override it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With