【发布时间】:2020-10-26 21:45:23
【问题描述】:
如果我在 XCode 12 Playground (Swift 5.3) 中运行以下代码,我会从两个列表中得到相同的结果:
import Foundation
var dict = NSMutableDictionary()
dict["x"] = 42
func stuff(_ d: inout NSMutableDictionary) {
d["x"] = 75
}
stuff(&dict)
dump(dict) // x is 75
另一个:
import Foundation
var dict = NSMutableDictionary()
dict["x"] = 42
func stuff(_ d: NSMutableDictionary) {
d["x"] = 75
}
stuff(dict)
dump(dict) // x is 75 still
根据此处的文档,第二个清单应该给我一个错误: https://docs.swift.org/swift-book/LanguageGuide/Functions.html
但它仍然有效。
这是因为这些 in-out 规则的执行仅限于 Swift 类型,而 Cocoa 类型除外?
【问题讨论】: