How to create an empty array in Swift?

前端 未结 13 931
耶瑟儿~
耶瑟儿~ 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:09

    Swift 5

    There are three (3) ways to create a empty array in Swift and shorthand syntax way is always preferred.

    Method 1: Shorthand Syntax

    var arr = [Int]()
    

    Method 2: Array Initializer

    var arr = Array()
    

    Method 3: Array with an Array Literal

    var arr:[Int] = []
    

    Method 4: Credit goes to @BallpointBen

    var arr:Array = []
    

提交回复
热议问题