Immutable/Mutable Collections in Swift

后端 未结 7 1416
不知归路
不知归路 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:38

    Arrays

    Create immutable array

    First way:

    let array = NSArray(array: ["First","Second","Third"])
    

    Second way:

    let array = ["First","Second","Third"]
    

    Create mutable array

    var array = ["First","Second","Third"]
    

    Append object to array

    array.append("Forth")
    


    Dictionaries

    Create immutable dictionary

    let dictionary = ["Item 1": "description", "Item 2": "description"]
    

    Create mutable dictionary

    var dictionary = ["Item 1": "description", "Item 2": "description"]
    

    Append new pair to dictionary

    dictionary["Item 3"] = "description"
    

    More information on Apple Developer

提交回复
热议问题