为什么我不能以这种方式在我的 [( String, [ProtocolType] )] 中插入一个 (String, [ProtocolConformer]) 元组?
Why can't I insert a (String, [ProtocolConformer]) tuple in my [( String, [ProtocolType] )] this way?
我在 playground 中浓缩了我的问题场景:
@objc protocol Prot {}
class Cla : Prot {}
var myArray: [ (aString: String, prot: [Prot] ) ] = []
var myProts: [Prot]? = [Cla()]
// error: type 'T' does not conform to protocol 'IntegerLiteralConvertible'
myArray.append(aString: "myname", prot: myProts!)
// works
myArray.append(aString: "myname", prot: myProts! as [Prot])
// works
if let myProts: [Prot] = myProts {
myArray.append(aString: "myname", prot: myProts)
}
为什么这里不接受强制解包?
Xcode6.1.1
只是编译器感到困惑 - 试试这个:
let tuple = (aString: "myname", prot: myProts!)
myArray.append(tuple)
出于某种原因,在某些情况下,它在传递给 function/method 时无法检测到元组。而且,正如经常发生的那样,错误消息并没有多大帮助。
我在 playground 中浓缩了我的问题场景:
@objc protocol Prot {}
class Cla : Prot {}
var myArray: [ (aString: String, prot: [Prot] ) ] = []
var myProts: [Prot]? = [Cla()]
// error: type 'T' does not conform to protocol 'IntegerLiteralConvertible'
myArray.append(aString: "myname", prot: myProts!)
// works
myArray.append(aString: "myname", prot: myProts! as [Prot])
// works
if let myProts: [Prot] = myProts {
myArray.append(aString: "myname", prot: myProts)
}
为什么这里不接受强制解包?
Xcode6.1.1
只是编译器感到困惑 - 试试这个:
let tuple = (aString: "myname", prot: myProts!)
myArray.append(tuple)
出于某种原因,在某些情况下,它在传递给 function/method 时无法检测到元组。而且,正如经常发生的那样,错误消息并没有多大帮助。