0
我需要改变未知深度的嵌套字典。Swift - 替换嵌套结构中的值
我意识到,在迅速结构是值类型的时候,其实我需要一个引用类型(“NSMutable”)
但我注意到,如果我访问使用点嵌套结构(。)语法我能够直接更新值,而不需要重新分配给原始“父母”。
例如在嵌套数组的情况:
var l1 = ["a0","b0"]
var l2 = ["a1","b1"]
var list = [l1,l2]
print(list)
>>[["a0", "b0"], ["a1", "b1"]]
// I can mutate the nested structs by using dot(.) syntax
// mutate the zero indexed nested array:
list[0].insert("x0", atIndex: 0)
print(list)
>> [["x0", "a0", "b0"], ["a1", "b1"]]
// try to mutate after assignment - Not able to
var l1Ref = list[0]
print(l1Ref)
>> ["x0", "a0", "b0"]
l1Ref.removeFirst()
print(l1Ref)
>> ["a0", "b0"]
print(list)
// still the same as was before
>> [["x0", "a0", "b0"], ["a1", "b1"]]
我怎么能发生变异的嵌套结构迭代,而无需使用点语法?