Morgan Wilde
Morgan Wilde

Reputation: 17333

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?

Upvotes: 2

Views: 620

Answers (1)

Antonio
Antonio

Reputation: 72800

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.

Upvotes: 2

Related Questions