[swift-users] Needing self when one initialiser method calls another.
Jo Albright
me at jo2.co
Fri Jan 15 00:35:14 CST 2016
Hi Diego,
Since you have not given the x & y variables initial values, it requires them to be set inside the init. You can write it like below instead of calling the other self.init
struct Vector {
var x: Double
var y: Double
init() {
x = 0
y = 0
}
init(x: Double, y: Double) {
self.x = y
self.y = y
}
}
Designer . Developer . Nerd
Jo Albright
> On Jan 14, 2016, at 11:48 PM, Diego Barros via swift-users <swift-users at swift.org> wrote:
>
> Hi all,
>
> Given the following example:
>
> struct Vector {
> var x: Double
> var y: Double
>
> init() {
> self.init(x: 0, y: 0)
> }
>
> init(x: Double, y: Double) {
> self.x = y
> self.y = y
> }
> }
>
> I'm just curious as to why the self (in bold) is needed in the init() that's calling init(x: Double, y: Double)? Can't the compiler infer from the parameters what should be called?
>
> - Diego
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160115/40d8b770/attachment.html>
More information about the swift-users
mailing list