What is the meaning of the '#' mark in swift language

Solution 1:

Update (Swift 3.*...)

the default behavior of the first parameter’s signature was changed drastically. To understand how argument labels (ex. “external parameters”) and parameter names (ex. “local parameters”) work, please read the chapter “Function Argument Labels and Parameter Names” from the Apple’s Swift-book.

Some examples:

func someFunction(parameterName: Int) { parameterName }
someFunction(parameterName: 5) // argument label not specified

func someFunction(argumentLabel parameterName: Int) { parameterName }
someFunction(argumentLabel: 5) // argument label specified

func someFunction(_ parameterName: Int) { parameterName }
someFunction(5) // argument label omitted

There is no difference in this behavior between methods and functions.


Update (Swift 2.*)

The feature described below was deprecated, one need to write the parameter name twice to get the same behavior as with hash symbol before.


Update (examples)

For functions: when the function is called and purpose of some parameters is unclear, you provide external names for those parameters.

func someFunction(parameterName: Int) { parameterName }
someFunction(5) // What is the meaning of "5"? 

func someFunction(externalParameterName parameterName: Int) { parameterName }
someFunction(externalParameterName: 5) // Now it's clear.

But if external and local names are the same, you just write a hash symbol before the parameter name.

func someFunction(#parameterName: Int) { parameterName }
// It's actually like:
// func someFunction(parameterName parameterName: Int) { parameterName }
someFunction(parameterName: 5)

For methods: by default first parameter name is only local (like by functions), but second and subsequent parameter names are both local and external (like as you write a hash symbol before the parameter name, this # is implicitly there):

class SomeClass {
    func someMethodWith(firstParameter: Int, andSecondParameter: Int) { ... }
}
SomeClass().someMethodWith(5, andSecondParameter: 10)

You can use # (or add an explicit external name) for the first parameter of the method too, but it'll not match Objective-C-style calling.

class SomeClass {
    func someMethodWith(#firstParameter: Int, andSecondParameter: Int) { ... }
}
SomeClass().someMethodWith(firstParameter: 5, andSecondParameter: 10)

Original answer

If you want to provide an external parameter name for a function parameter, and the local parameter name is already an appropriate name to use, you do not need to write the same name twice for that parameter. Instead, write the name once, and prefix the name with a hash symbol (#). This tells Swift to use that name as both the local parameter name and the external parameter name.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/ru/book/swift-programming-language/id881256329?l=en&mt=11

Solution 2:

This has changed in Swift 2:

now you specify an external parameter name before the internal one, instead of using # to force the existing name.


According to the convention, the method name contains the action verb and the first parameter is not specified when calling the method:

func sayHiFrom(sender: String, to: String) {
    print("Hi from \(sender) to \(to)!")
}

sayHiFrom("Jules", to: "Jim")

Specifying an internal parameter name

This time the second parameter has a different name for using inside the method, without changing the external name. When there is two names for a parameter, the first one is external and the second one is internal:

func sayHiFrom(sender: String, to receiver: String) {
    print("Hi from \(sender) to \(receiver)!")
}

sayHiFrom("Jane", to: "John")

Forcing an external parameter name

You can force the first parameter to have an external name:

func sayHi(from sender: String, to receiver: String) {
    print("Hi from \(sender) to \(receiver)!")
}

sayHi(from: "Joe", to: "Jack")

In this case, it's better that the method name does not contain the action term, because the forced parameter name already plays its role.

Forcing no external parameter names

You can also remove the parameter name for other parameters by preceding them with _ (underscore):

func sayHi(sender: String, _ receiver: String) {
    print("Hi from \(sender) to \(receiver)!")
}

sayHi("Janice", "James")