字典同数组一样,在赋值或参数传递过程中会发生复制行为。下面我们通过一个例子来说明字典的复制:
1| var addressDict = ["上海" : "黄浦区","广东" : "深圳","江苏" : "南京"]
2| var copyDict = addressDict
3| addressDict["上海"] = "浦东新区"
4| print(addressDict)
5| print(copyDict)
运行结果如下:
["江苏": "南京", "上海": "浦东新区", "广东": "深圳"]
["江苏": "南京", "上海": "黄浦区", "广东": "深圳"]
字典和数组一样都是值类型,字典的复制和数组一致,当原来的字典的键值对发生改变的时候,复制的字典的键值对不会发生改变。
网友评论