ios - Merge Swift Array with same superclass -


i'm trying find best way merge swift arrays, not of same type, have same superclass. i've read tutorials know can use:

var array = ["some", "array", "of", "strings"] array += ["another string", "and 1 more"] array.append(["or", "this"]) 

in case array variable infers type [string]. problem have relates next hypothetical situation classes (properties not matter in case, there make difference between classes):

class {     var property1 : string? }  class b : {     var property2: string? }  class c : {     var property3: string? } 

so create array of class a , b objects:

var array = [ a(), b(), a(), b() ] 

this array should of type [a], since inferred type both a , b classes.

now want append objects array, of type c.

var anotherarray = [ c(), c(), c() ]

since anotherarray of type [c], should still able append it, since c instances respond a methods. try:

array += anotherarray 

this fails due to:

binary operator '+=' cannot applied operands of type '[a]' , '[c]'. 

similar story using append method. while make sense, cannot understand why couldn't work.

can explain why not working? best solution problem?

the sensible solution found define type of anotherarray [a], there better ones or correct?

var anotherarray : [a] = [ c(), c(), c() ] 

thanks!

if c inherits a can "upcast" array of type [c] array of type [a]

array += anotherarray [a] 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -