Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler Warning in XCode6 Beta - “Shorthand External Parameter Names”

Tags:

ios

swift

xcode6

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?

like image 220
Raz Avatar asked Jan 21 '26 10:01

Raz


1 Answers

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.

like image 82
Gabriele Petronella Avatar answered Jan 24 '26 04:01

Gabriele Petronella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!