Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an ampersand (&) mean in the Swift language?

I know about the ampersand as a bit operation but sometimes I see it in front of variable names. What does putting an & in front of variables do?

like image 942
applejuiceteaching Avatar asked May 30 '15 01:05

applejuiceteaching


People also ask

What does an ampersand represent?

An ampersand is a sign for the word and. It's written or typed as the symbol &. It's a modification of the term “and per se and,” which has Latin origins. The ampersand can indicate that the listed items are grouped together as part of a name.

What is this symbol called &?

The ampersand, also known as the and sign, is a letter that is the logogram &, representing the conjunction "and". It originated as a ligature of the letters et—Latin for "and".

What is the use of & symbol?

What is an &? & is called an ampersand symbol (pronounced “AM- per-sand”). Essentially, it means “and”. It is used both (a) in the body of the paper as part of a citation and (b) at the end of the paper as part of a reference.

What is an example of ampersand?

Although ampersands are thought of as informal, if the ampersand is officially part of a company name, it's best to use the ampersand instead of writing out the word “and.” For example, you write “Tiffany & Co.,” “Procter & Gamble,” and “AT&T” with ampersands.


4 Answers

It means that it is an in-out variable. You can do something directly with that variable. It is passed by address, not as a copy.

For example:

var temp = 10
func add(inout a: Int){
    a++
}
add(inout:&temp)
temp // 11
like image 29
Leo Avatar answered Oct 17 '22 06:10

Leo


It works as an inout to make the variable an in-out parameter. In-out means in fact passing value by reference, not by value. And it requires not only to accept value by reference, by also to pass it by reference, so pass it with & - foo(&myVar) instead of just foo(myVar)

As you see you can use that in error handing in Swift where you have to create an error reference and pass it to the function using & the function will populate the error value if an error occur or pass the variable back as it was before

Why do we use it? Sometimes a function already returns other values and just returning another one (like an error) would be confusing, so we pass it as an inout. Other times we want the values to be populated by the function so we don't have to iterate over lots of return values, since the function already did it for us - among other possible uses.

I hope that helps you!

like image 133
Icaro Avatar answered Oct 17 '22 06:10

Icaro


There's another function of the ampersand in the Swift language that hasn't been mentioned yet. Take the following example:

protocol Foo {}
protocol Bar {}

func myMethod(myVar: Foo & Bar) {
    // Do something
}

Here the ampersand syntax is stating that myVar conforms to both the Foo and Bar protocol.

As another use case, consider the following:

func myMethod() -> UIViewController & UITableViewDataSource {
    // Do something
}

Here we're saying that the method returns a class instance (of UIViewController) that conforms to a certain protocol (UITableViewDataSource). This is rendered somewhat obsolete with Swift 5.1's Opaque Types but you may see this syntax in pre-Swift 5.1 code from time to time.

like image 16
WongWray Avatar answered Oct 17 '22 07:10

WongWray


If you put & before a variable in a function, that means this variable is inout variable.

@Icaro already described what it means, I will just give an example to illustrate the difference between inout variables and in variables:

func majec(inout xValue:Int, var yValue:Int) {
    xValue = 100
    yValue = 200
}

var xValue = 33
var yValue = 33
majec(&xValue, yValue: yValue)
xValue //100
yValue //33
like image 11
William Kinaan Avatar answered Oct 17 '22 07:10

William Kinaan