Search code examples
iosswifttuplesxcode6.1forced-unwrapping

Why can't I insert a (String, [ProtocolConformer]) tuple in my [( String, [ProtocolType] )] this way?


I've condensed my problem scenario in 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)
}

Why is forced unwrap not accepted here?

Xcode 6.1.1


Solution

  • It's just the compiler getting confused - try this:

    let tuple = (aString: "myname", prot: myProts!)
    myArray.append(tuple)
    

    For some reason, and in some cases, it is not able to detect a tuple when passed to a function/method. And, as often happening, the error message doesn't help much.