我想创建一个函数来交换 2 个变量!但是对于新的 swift,我不能使用“var”....
import UIKit
func swapF(inout a:Int, inout with b:Int ) {
print(" x = \(a) and y = \(b)")
(a, b) = (b, a)
print("New x = \(a) and new y = \(b)")
}
swapF(&5, with: &8)
最佳答案
文字不能作为 inout 参数传递,因为它们本质上是不可变的。
改用两个变量:
var i=5
var j=8
swapF(a:&i, with: &j)
此外,对于最后的 Swift 3 快照之一,inout 应该放在类型附近,函数的原型(prototype)变为:
func swapF(a:inout Int, with b:inout Int )
关于ios - 无法将不可变值作为 inout 参数 : literals are not mutable, 传递,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295620/