Reputation: 14340
I have an array like so:
var array: [(NSDate?, NSData?)] = []
And when I use it like this:
array.append(NSDate(), nil)
The compiler gives the error accessing members of protocol type value 'NSData' is unimplemented
I don't understand this error, it should work right?
Upvotes: 0
Views: 150
Reputation: 12768
It is having difficulty determining the type of the tuple provided in swift when passing nil as a parameter so you must cast it to be the correct type.
var array: [(NSDate?, NSData?)] = []
array.append((NSDate(), nil) as (NSDate?, NSData?))
or
let t : (NSDate?, NSData?) = (NSDate(), nil)
array.append(t)
Upvotes: 4
Reputation: 51911
Swift doesn't support conversion like this:
let a:(String, Int?) = ("test", 1)
let b:(String?, Int?) = a
// ^ error: cannot express tuple conversion '(String, Int?)' to '(String?, Int?)'
The tuple type must be exactly matched to the destination type.
But, In your case, it can be considered as a bug. because this works:
func foo(x:(NSDate?, NSData?)) { println(x) }
foo((NSDate(), nil))
But this doesn't:
struct Foo<T> {
func foo(x:T) { println(x) }
}
var f = Foo<(NSDate?, NSData?)>()
f.foo((NSDate(), nil))
The workaround would be:
array.append((NSDate(), nil) as (NSDate?, NSData?))
// OR
array.append((NSDate() as NSDate?, nil as NSData?))
// OR
array.append(NSDate() as NSDate?, nil as NSData?)
Upvotes: 1