How to create an empty array in Swift?

前端 未结 13 951
耶瑟儿~
耶瑟儿~ 2020-11-30 17:25

I\'m really confused with the ways we create an array in Swift. Could you please tell me how many ways to create an empty array with some detail?

13条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 18:18

    Initiating an array with a predefined count:

    Array(repeating: 0, count: 10)

    I often use this for mapping statements where I need a specified number of mock objects. For example,

    let myObjects: [MyObject] = Array(repeating: 0, count: 10).map { _ in return MyObject() }

提交回复
热议问题