Question

I'm playing with Swift on Xcode6 Beta, and when I'm using in a func definition '#', just like in apple's Swift programing guide, i'm getting the following compiler error:

Extraneous '#' in parameter: 'characterToFind' is already the keyword argument name

// this code is a copy-paste code from apple's Swift language programing guide

func containsCharacter(#string: String, #characterToFind: Character) -> Bool {
    for character in string {
        if character == characterToFind {
            return true
        }
    }
    return false
}

Is it just a bug with the new Xcode?

Was it helpful?

Solution

If it's a method (a func within a class), then the # is redundant and I suppose this is what the compiler means.

In methods, the first argument name is assumed to be only local, whereas the others are automatically assumed as both external and local names, as if you had written the # in front of them.

So, instead of

func containsCharacter(#string: String, #characterToFind: Character) -> Bool

you can write

func containsCharacter(#string: String, characterToFind: Character) -> Bool

and it's going to be exactly the same thing.

To further prove this point, I tried this in the playground

func foo(#x: Int, #y: Int) { }
class foobar {
    func foo(#x: Int, #y: Int)
    func bar(#x: Int, y: Int)
}

The three functions are identical, but the compiler raises a warning on the second foo

Extraneous '#' in parameter: 'characterToFind' is already the keyword argument name

Again, the parameters after the first in methods are already automatically considered external names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top