Andrei Durnea Andrei Durnea - 6 months ago 36
Swift Question

Have outlets in extensions

What is the reason we can't have the outlets for views in a ViewController's extension?

In the and it's the same class referenced by the same xib.

As described in the swift guide from apple:

Extensions in Swift can:

Add computed instance properties and computed type properties

Define instance methods and type methods

Provide new initializers

Define subscripts

Define and use new nested types

Make an existing type conform to a protocol

I presume as suggested in the comments that it has to do something with memory. While a new stored property will increase the memory for that object a computed one will not.

Answer Source

According the documentation, extensions cannot add stored properties to the class:

Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.

Outlets are stored properties that are populated by the storyboards. Therefore you cannot define an outlet in the extension.

You can take a look at following SO question for some reasoning behind not allowing stored properties on extensions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download