How do you create an immutable array in Swift?

后端 未结 5 815
臣服心动
臣服心动 2020-11-30 04:16

How do I create an immutable array in Swift?

A superficial reading of the docs would suggest you can just do

let myArray = [1,2,3]

5条回答
  •  青春惊慌失措
    2020-11-30 04:24

    It is now possible.

    From Apple Developer

    If you assign an array or a dictionary to a constant, that array or dictionary is immutable, and its size and contents cannot be changed.

    So now

    let myArray = [1,2,3]
    

    produces completely immutable array. Yay!

提交回复
热议问题