Merge Swift Array with same superclass

前端 未结 3 1578
一个人的身影
一个人的身影 2020-12-19 08:25

I\'m trying to find the best way to merge Swift arrays, that are not of same type, but they have same superclass. I\'ve already read all the tutorials so I know I can use:

3条回答
  •  粉色の甜心
    2020-12-19 09:02

    If C inherits from A then you can "upcast" an array of type [C] to an array of type [A]

    array += anotherArray as [A]
    

    Alternatively, use (tested with Swift 4)

    array.append(contentsOf: anotherArray)
    

提交回复
热议问题