How to define initializers in a protocol extension?

SwiftInitializationSwift2ProtocolsSwift Extensions

Swift Problem Overview


protocol Car {
     var wheels : Int { get set}

     init(wheels: Int)

}

extension Car {
    init(wheels: Int) {
        self.wheels = wheels
    }
}

on self.wheels = wheels i get the error

Error: variable 'self' passed by reference before being initialized

How can I define the initializer in the protocol extension?

Swift Solutions


Solution 1 - Swift

As you can see this doesn't work under these circumstances because when compiling, one has to make sure that all properties are initialized before using the struct/enum/class.

You can make another initializer a requirement so the compiler knows that all properties are initialized:

protocol Car {
	var wheels : Int { get set }
    // make another initializer
    // (which you probably don't want to provide a default implementation)
    // a protocol requirement. Care about recursive initializer calls :)
	init()
	init(wheels: Int)
	
}

extension Car {
    // now you can provide a default implementation
	init(wheels: Int) {
		self.init()
		self.wheels = wheels
	}
}

// example usage

// mark as final
final class HoverCar: Car {
	var wheels = 0
	init() {}
}

let drivableHoverCar = HoverCar(wheels: 4)
drivableHoverCar.wheels // 4

As of Xcode 7.3 beta 1 it works with structs as expected but not with classes since if they are not final the init(wheels: Int) in the protocol is a required init and it can be overridden therefore it cannot be added through an extension. Workaround (as the complier suggests): Make the class final.

Another workaround (in depth; without final class)

To work with classes without making them final you can also drop the init(wheels: Int) requirement in the protocol. It seems that it behaves no different than before but consider this code:

protocol Car {
	var wheels : Int { get set }
	init()
    // there is no   init(wheels: Int)
}

extension Car {
	init(wheels: Int) {
		self.init()
		print("Extension")
		self.wheels = wheels
	}
}

class HoverCar: Car {
	var wheels = 0
	required init() {}
	init(wheels: Int) {
		print("HoverCar")
		self.wheels = wheels
	}
}

// prints "HoverCar"
let drivableHoverCar = HoverCar(wheels: 4)

func makeNewCarFromCar<T: Car>(car: T) -> T {
	return T(wheels: car.wheels)
}

// prints "Extension"
makeNewCarFromCar(drivableHoverCar)

So if you make a Car from a generic context where the type on which you call init is only to be known as Car the extension initializer is called even though an initializer is defined in HoverCar. This only occurs because there is no init(wheels: Int) requirement in the protocol.

If you add it you have the former problem with declaring the class as final but now it prints two times "HoverCar". Either way the second problem probably never occurs so it might be a better solution.

Sidenote: If I have made some mistakes (code, language, grammar,...) you're welcome to correct me :)

Solution 2 - Swift

My understanding is that this isn't possible, because the protocol extension can't know which properties the conforming class or struct has - and therefore cannot guarantee they are correctly initialized.

If there are ways to get around this, I'm very interested to know! :)

Solution 3 - Swift

@Qbyte is correct.

In addition, you can take a look at my Configurable

In that I have Initable protocol

public protocol Initable {
    // To make init in protocol extension work
    init()
}

public extension Initable {
    public init(@noescape block: Self -> Void) {
        self.init()
        block(self)
    }
}

Then in order to conform to it

extension Robot: Initable { }

I have 2 ways, using final or implement init

final class Robot {
    var name: String?
    var cute = false
}

class Robot {
    var name: String?
    var cute = false

    required init() {

    }
}

Solution 4 - Swift

May not be the same but in my case instead of using init I used a static func to return the object of the class.

protocol Serializable {
   static func object(fromJSON json:JSON) -> AnyObject?
}

class User {
    let name:String
    
    init(name:String) {
        self.name = name
    }
}

extension User:Serializable {

    static func object(fromJSON json:JSON) -> AnyObject? {
        guard let name = json["name"] else {
            return nil
        }

        return User(name:name)
     }

}

Then to create the object I do something like:

let user = User.object(fromJSON:json) as? User

I know its not the best thing ever but its the best solution I could find to not couple business model with the data layer.

NOTE: I'm lazy and I coded everything directly in the comment so if something doesn't work let me know.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbogenView Question on Stackoverflow
Solution 1 - SwiftQbyteView Answer on Stackoverflow
Solution 2 - SwiftEivind Rannem BøhlerView Answer on Stackoverflow
Solution 3 - Swiftonmyway133View Answer on Stackoverflow
Solution 4 - SwiftMijailView Answer on Stackoverflow