Swiftformat: IBOutlet variables get 'weak' removed from declaration

Created on 15 Sep 2020  路  2Comments  路  Source: nicklockwood/SwiftFormat

One of the actions that _SwiftFormat_ took was removing weak from my IBOutlet variable declaration:

-       @IBOutlet weak var someField: NSTextField!
+       @IBOutlet var someField: NSTextField!

According to the Apple documentation (scroll down to _Store References to Important Views_), these should be defined as weak:

In Swift, include the weak keyword to prevent your view controller from holding a second strong reference to the view鈥攖he first originates from the view hierarchy itself.

This to me is a bug and hopefully gets addressed. In the meantime, is there a switch to turn this off? Thanks!

not a bug

Most helpful comment

This is deliberate, as per Apple's recommendation in the WWDC video (https://developer.apple.com/videos/play/wwdc2015/407/ @ 32:30).

This transform should be safe in virtually all cases, and slightly improves performance. Prior to iOS 6 (when the behavior of viewDidUnload changed), using weak was recommended to save memory, but that no longer applies. I suspect that the Apple documentation you linked to just hasn't been updated.

If you prefer to turn this off, you can do so by disabling the strongOutlets rule. I suggest taking a look at the Rules documentation to get a better understanding of the rules that SwiftFormat applies by default and how to disable them.

You can also SwiftFormat run with the --lint or --verbose option to find out which rules are being applied to which files if you aren't sure how to disable a particular change.

All 2 comments

This is deliberate, as per Apple's recommendation in the WWDC video (https://developer.apple.com/videos/play/wwdc2015/407/ @ 32:30).

This transform should be safe in virtually all cases, and slightly improves performance. Prior to iOS 6 (when the behavior of viewDidUnload changed), using weak was recommended to save memory, but that no longer applies. I suspect that the Apple documentation you linked to just hasn't been updated.

If you prefer to turn this off, you can do so by disabling the strongOutlets rule. I suggest taking a look at the Rules documentation to get a better understanding of the rules that SwiftFormat applies by default and how to disable them.

You can also SwiftFormat run with the --lint or --verbose option to find out which rules are being applied to which files if you aren't sure how to disable a particular change.

Thanks! It's odd that the IB sets _weak_ by default. The video was enlightening. Please feel free to close issue, keeping variable strong.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PompeiaPaetenari picture PompeiaPaetenari  路  3Comments

Cyberbeni picture Cyberbeni  路  4Comments

fellipecaetano picture fellipecaetano  路  4Comments

PompeiaPaetenari picture PompeiaPaetenari  路  4Comments

shawnkoh picture shawnkoh  路  3Comments