我正在从Swift 2转换到Swift 3。 我注意到在Swift 3中无法将布尔值转换为整数值。
let p1 = ("a" == "a") //true
print(true) //"true\n"
print(p1) //"true\n"
Int(true) //1
Int(p1) //error
例如,在Swift 2中,这些语法是有效的。但在Swift 3中,print(p1)
会产生错误。
错误是error: cannot invoke initializer for type 'Int' with an argument list of type '((Bool))'
我理解为什么会出现这些错误。有人能解释一下这种安全性的原因以及如何在Swift 3中将布尔值转换为整数吗?