"swap(&someInt, &anotherInt)" 中的“&”。它代表什么?它的作用是什么?
"&" in "swap(&someInt, &anotherInt)". What does it represent? What is its function?
我是 Swift 的新手,我正在尝试学习 'inout' 关键字的概念。我在 "the swift programming language 2.1" 中看到了这段代码。我的问题是,为什么 "swap(&someInt, &anotherInt)" 中有一个“&”。它代表什么?它的作用是什么?
func swapTwoInts(inout a: Int, inout _ b: Int){
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swap(&someInt, &anotherInt)
print("someInt is now \(someInt) and anotherInt is now \(anotherInt)")
就像在 C++ 中通过引用传递一样,代码调用位中的 & 符号只是告诉编译器您允许函数 swapTwoInts
更改 someInt
和 anotherInt
.如果你没有把 & 号放在那里,代码就不会编译。
我是 Swift 的新手,我正在尝试学习 'inout' 关键字的概念。我在 "the swift programming language 2.1" 中看到了这段代码。我的问题是,为什么 "swap(&someInt, &anotherInt)" 中有一个“&”。它代表什么?它的作用是什么?
func swapTwoInts(inout a: Int, inout _ b: Int){
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swap(&someInt, &anotherInt)
print("someInt is now \(someInt) and anotherInt is now \(anotherInt)")
就像在 C++ 中通过引用传递一样,代码调用位中的 & 符号只是告诉编译器您允许函数 swapTwoInts
更改 someInt
和 anotherInt
.如果你没有把 & 号放在那里,代码就不会编译。