Swift beta 5增加了这一功能,您在几次try 中就已经掌握了这一新方法.展开运算符!
和?
现在将值传递给运算符或方法调用.也就是说,您可以通过以下任何方式向该数组中添加:
dict["key"]! += [4]
dict["key"]!.append(4)
dict["key"]?.append(4)
和往常一样,要注意使用哪种运算符——强制展开字典中没有的值会导致运行时错误:
dict["no-key"]! += [5] // CRASH!
然而,使用可选链接将以静默方式失败:
dict["no-key"]?.append(5) // Did it work? Swift won't tell you...
理想情况下,您可以使用新的空合并运算符??
来处理第二种情况,但现在这不起作用.
Answer from pre-Swift beta 5:
这是Swift 的一个怪癖,你不可能做你想做的事.问题是,任何可选变量的value实际上都是一个常数——即使在强制展开时也是如此.如果我们只定义一个可选数组,下面是我们可以做的和不能做的:
var arr: Array<Int>? = [1, 2, 3]
arr[0] = 5
// doesn't work: you can't subscript an optional variable
arr![0] = 5
// doesn't work: constant arrays don't allow changing contents
arr += 4
// doesn't work: you can't append to an optional variable
arr! += 4
arr!.append(4)
// these don't work: constant arrays can't have their length changed
使用字典时遇到问题的原因是,订阅字典会返回一个可选值,因为无法保证字典将具有该键.因此,字典中的数组与上面的可选数组具有相同的行为:
var dict = Dictionary<String, Array<Int>>()
dict["key"] = [1, 2, 3]
dict["key"][0] = 5 // doesn't work
dict["key"]![0] = 5 // doesn't work
dict["key"] += 4 // uh uh
dict["key"]! += 4 // still no
dict["key"]!.append(4) // nope
如果需要更改字典中数组中的某个内容,则需要获取该数组的副本,进行更改,然后重新分配,如下所示:
if var arr = dict["key"] {
arr.append(4)
dict["key"] = arr
}
ETA:同样的技术也适用于Swift beta 3,不过常量数组不再允许更改内容.