jmtrachy
jmtrachy

Reputation: 61

Odd xcode error when calling defined func in Swift

I have the following function defined in a .swift file in a new project I'm creating:

func createCircle(xPos: Int, yPos: Int) {
    // do code here
}

For some reason when I try to call it using the following code xcode displays an error stating "Missing argument label 'yPos:' in call".

createCircle(100, 100)

The odd thing is it treats yPos different than xPos - if I include xPos: in the calling function it highlights the line and says "Cannot convert the expression's type '$Tf' to type 'IntegerLiteralConvertable'". So the following is what I end up with in order to call the aforementioned function:

createCircle(100, yPos: 100)

Am I missing something obvious or is this an xcode beta bug?

Upvotes: 0

Views: 329

Answers (1)

TheLazyChap
TheLazyChap

Reputation: 1902

According to the Swift documentation

Section - Methods - Local and External Parameter Names for Methods

"Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default."

Judging from your error, it seems like you're using a method and not a function. You don't need to provide the name for the first parameter but you must for subsequent parameters but this behaviour can be changed with the _ (underscore character).

class MyCircle {
    var xPos: Double = 0.0;
    var yPos: Double = 0.0;

    func createCircle(x: Double, _ y: Double) {
        // insert create circle logic here.
    }
}

var cir = MyCircle();
cir.createCircle(100, 100);

By using the underscore for subsequent parameters you don't have to provide the label for them when calling the method.

Upvotes: 0

Related Questions