【发布时间】:2015-03-09 00:10:54
【问题描述】:
我有一个(String,Bool) 元组数组:
var names : [(name:String, important:Bool)] = [("Paul",true), ("Peter",false)]
我还有一个UISwitch 控件:
@IBOutlet weak var importantSwitch: UISwitch!
我想添加到数组中,使用开关状态来确定Bool。
以下操作不起作用:
names.append( name: "whatever", important: importantSwitch.on )
XCode 报告的问题是“类型 T 不符合协议 IntegerLiteralConvertible”。
这些确实有效:
let i : Bool = importantSwitch.on
names.append( name: "whatever", important: i )
或
names.append( name: "whatever", important: importantSwitch.on==true )
我的问题本质上是,为什么?
【问题讨论】:
-
带有元组的数组在 Swift 中仍然很奇怪,类似的例子请参见 stackoverflow.com/questions/24539679/…。定义类型别名在这里也有帮助。 (但这并不能回答“为什么”。)
-
@MartinR 好的,我想我已经抓住了“为什么”。