Swift Protocol Arrays Adding vs. Instantiating -
in swift 3.0 xcode beta 3, defined simple protocol , 2 structs implement it, if initialize array when creating objects, works, if try add elements error:
cannot convert value of type '[h]' expected argument type 'inout _'
shouldn't work?
protocol h { var v : int { set } func hello() } struct j : h { var v : int func hello() { print("j") } } struct k : h { var v : int func hello() { print("k") } } let ag:[h] = [k(v:3), j(v:4)] // works ag[0].hello() ag[1].hello() var af:[h] = [] af += [k(v:3)] // not work af += [j(v:4)] // not work af[0].hello() af[1].hello()
it's type issue. need things add af
same type af
, namely [h]
:
var af:[h] = [] let arr1:[h] = [k(v:3)] let arr2:[h] = [j(v:4)] af += arr1 af += arr2
Comments
Post a Comment