【发布时间】:2016-09-06 01:35:10
【问题描述】:
感谢您花时间和精力帮助我解决这个问题
我注意到,对于集合类型(数组、集合和字典),remove 方法总是返回一个值,而 insert 方法没有,为什么会这样,为什么它们都不返回一个值。这背后的设计逻辑是什么?我想知道。
var letterSet = Set<String>()
print(letterSet.insert("a")) // -> ()
print(letterSet.remove("a")) // -> Optional("a")
var intArray = [Int]()
print(intArray.insert(5, atIndex: 0)) // -> ()
print(intArray.removeAtIndex(0)) // -> 5
var colorDic = [String : String]()
print(colorDic.updateValue("Blue", forKey: "FirstColor")) // -> nil
print(colorDic.removeValueForKey("FirstColor"))// -> Optional("Blue")
顺便说一句:为什么 Dictionary 的 updateValue 方法返回 nil 而不是 void
非常感谢
【问题讨论】:
标签: arrays swift dictionary collections set