Swift protocol extension self reference issues with init

Jeef

I'm looking for a way to add a default initializer to a protocol via protocol extensions.

My protocol is:

protocol TestProtocol {
    var myVar : Double { get set }
    init(value: Double)
    init(existingStruct : TestProtocol)
}

I've implemented a struct using this protocol as:

struct TestStruct : TestProtocol {
    var myVar : Double

    init(value : Double) {
        myVar = value
    }

    init (existingStruct : TestProtocol) {
        myVar = existingStruct.myVar
    }
}

However if I try via extension to make a default initializer for this protocol I run into self issues:

extension TestProtocol {
    init(value : Double) {
        myVar = value
    }

    init(existingStruct : TestProtocol) {
        myVar = existingStruct.myVar
    }
}

Where both assignment lines issue the error Variable 'self' passed by reference before being initialized

Is there a way to make this work - or am i limited to using classes?

enter image description here

DevAndArtist

Your question is almost the same as in this post I answered yesterday.

Here is the trick to solve this :)

protocol TestProtocol {
    var myVar : Double { get set }
    init() // designated initializer which will ensure that your class or structer type will instantiate correctly
}

struct TestStruct : TestProtocol {
    var myVar : Double

    init() {
        myVar = 0
    }
}

extension TestProtocol {
    init(value : Double) {
        self.init()
        myVar = value
    }

    init(existingStruct : TestProtocol) {
        self.init()
        myVar = existingStruct.myVar
    }
}

Have a good day. :) Protocol extension is so nice.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift protocol with "where Self" clause

what is 'where self' in protocol extension

Swift: Is it possible to add a protocol extension to a protocol?

Dispatching to Swift protocol extension

Swift extension that conforms to protocol

Swift protocol defining class method returning self

String extension that modify self in swift

Swift 2: UITableViewDataSource protocol extension

Swift 2 Error using mutating function in Protocol extension "Cannot use mutating member on immutable value: 'self' is immutable

Swift property observer in protocol extension?

Swift: Providing a default protocol implementation in a protocol extension

Swift protocol extension with property conforming to protocol

Swift Self as associated type bound in protocol

Protocol Extension Initializer forcing to call self.init

Swift type erasure protocol with required init method

Swift Protocol using associated types and Self?

Swift protocol extension where Self: Equatable doesn't work

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

Swift generic extension with self casting

Swift: Protocol `static var foo: Self` and enums

init let properties with reference to self in subclass in swift

Objective C syntax corresponding to Swift "extension where self: <some protocol>"

Swift : protocol extension and arrays

Not able to pass self (which implements a protocol) to init method of a class instantiation.(Swift)

Swift overriding protocol extension keeping the extension behaviour

Swift: "Ambiguous reference to member" init

"self as?" within protocol extension

Swift protocol extension with specific Self type

How can I use self for init in extension in Swift?