我想创建一个函数来交换两个变量!但对于新的 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 - 无法将不可变值作为输入参数 : literals are not mutable, 传递,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54815558/