Swift: different objects in one array?

后端 未结 4 1219
鱼传尺愫
鱼传尺愫 2021-02-06 14:06

Is there a possibility to have two different custom objects in one array?

I want to show two different objects in a UITableView and I think the easiest way

4条回答
  •  感动是毒
    2021-02-06 15:01

    You can use AnyObject array to hold any kind of objects in the same array:

    var objectsArray = [AnyObject]()
    objectsArray.append("Foo")
    objectsArray.append(2)
    
    // And also the inmutable version
    let objectsArray: [AnyObject] = ["Foo", 2]
    
    // This way you can let the compiler infer the type
    let objectsArray = ["Foo", 2]
    

提交回复
热议问题