Immutable/Mutable Collections in Swift

后端 未结 7 1426
不知归路
不知归路 2020-11-29 18:44

I was referring to Apple\'s Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could\'t

7条回答
  •  日久生厌
    2020-11-29 19:25

    There is only one Array and one Dictionary type in Swift. The mutability depends on how you construct it:

    var mutableArray = [1,2,3]
    let immutableArray = [1,2,3]
    

    i.e. if you create an assign to a variable it is mutable, whereas if you create an assign to constant it is not.

    WARNING: Immutable arrays are not entirely immutable! You can still change their contents, just not their overall length!

提交回复
热议问题