init let properties with reference to self in subclass in swift

jonathan topf

I'm learning swift and coming across the following problem very often. I will have a class with a property that feels as if it should be a let property as it will only be set once. I'd also like this child object to maintain a reference to its owner, which should also be a let property, as the parent relationship will not change. The problem arises when the parent class subclasses another class and all let properties need to be set before super.init () is run but requires a reference to self to init.

Here's a quick example

class NodeView: UIView {
    let _nodePlugView: NodePlugView

    init (node: Node) {
        _nodePlugView = NodePlugView (parentView: self)
        super.init ()
    }
}

Of course, I could just use var for these _nodePlugView but it doesn't feel quite right. Is there another pattern that people would recommend?

Chris

This is a good use case for an implicitly unwrapped optional.

The Apple documentation states:

“Implicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter. The primary use of implicitly unwrapped optionals in Swift is during class initialization.”

This means that you can use an implicitly unwrapped optional when a property depends on some aspect of self (but not the superclass) when it is initialised.

The property will be nil until it is set in the init() method and then must never be nil again.

class NodeView: UIView {

    var _nodePlugView: NodePlugView!

    init (node: Node) {
        super.init()
        _nodePlugView = NodePlugView (parentView: self)
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift protocol extension self reference issues with init

NSManagedObject subclass with failable init in swift

Swift subclass' init must set all properties before calling super but property requires super's init to complete first

swift : shortcut for guard "let self = ..."?

Swift: How to initialise a let constant in a UIViewController subclass

Call parent constructor when init a subclass in Swift

Swift: how to order instructions in init method of a subclass

Swift: Get subclass variable after init in switch

Swift: "Ambiguous reference to member" init

How to subclass a UIViewController and add properties in swift?

Automatically Add Self Reference To Properties Of Object

Error in Swift class: Property not initialized at super.init call - How to initialize properties which need use of self in their initializer parameter

Swift: Call self method inside init

Subclass of NSViewController: use of 'self' in delegating initializer before self.init is called Error

Why does Swift disallow assignment to self in class init, but not in protocol init?

Swift: Convenience initializers - Self used before self.init call

Difference between using self and not using self in Swift init

Swift 5: 'self' used before 'self.init' call

How do I write a custom init for a UIView subclass in Swift?

Calling super.init() in initializer of NSObject subclass in Swift

Does a swift subclass *always* have to call super.init()

Swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView'

Can we edit properties of "let" protocol properties in Swift?

Reference `self` in Swift instance member declaration

How to reference self in a Subscriber in Combine in Swift

Reference to self with Swift 4's KeyPath

Swift: How to deallocate properties when init throw?

Swift - Setting properties in init method using closures

'let' property may not be initialized directly; use "self.init(...)" or "self = ..." instead