Morgan Wilde Morgan Wilde - 1 year ago 172
iOS Question

Extra argument in call

I'm struggling with this part of Swift, nowhere do I see an extra argument in that method call.

struct RectPadding {
var top: CGFloat
var right: CGFloat
var bottom: CGFloat
var left: CGFloat

init() {
top = 0
right = 0
bottom = 0
left = 0
}

func setPadding(each: CGFloat) {
setPadding(top: each, right: each, bottom: each, left: each) // I get the error here
}
mutating func setPadding(#top: CGFloat, right: CGFloat, bottom: CGFloat, left: CGFloat) {
self.top = top
self.right = right
self.bottom = bottom
self.left = left
}
}


What am I missing?

Answer Source

This method:

mutating func setPadding(each: CGFloat) {
    setPadding(top: each, right: each, bottom: each, left: each) // I get the error here
}

has to be declared as mutating, because in turn it invokes a mutating method.

The reason of the error message (extra argument) is because it tries to call itself (which has the same name), which accepts just one parameter.

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