我试图使用包含可空int的散列作为值。以下是代码。
var facs:[(pk:Int,phone:Int?,name:String)] = []
var phone: AnyObject? = fac["phone"]!
var phoneLong:Int?;
if(phone == nil){
phoneLong = nil
}
else{
phoneLong = phone as? Int
}
var id = fac["id"]! as Int
var name = fac["name"]! as String
facs.append(pk:id, phone:phoneLong, name:name)
但是,我在facs.append
行上收到编译错误,指出Type 'T' does not conform to protocol 'IntegerLiteralConvertible'
。我尝试了一些变化,我唯一可以摆脱错误的方法是让手机成为一个不可空的int,这不是我需要的。想法?
答案 0 :(得分:2)
看起来append
方法没有正确地将参数检测为元组 - 只需通过将元组赋值给变量来明确它:
let params = (pk:id, phone:phoneLong, name:name)
facs.append(params)