我有一个函数,它接受一个 json 对象,其内容可以是任何类型(字典、数组、字符串等)并根据类型修改对象。
在下面的人为示例函数“foo”中,如何修改字典?我得到编译器错误:
error: '@lvalue $T6' is not identical to '(String, String)'
这是功能
func foo (var item: AnyObject) {
// ... other logic that handles item of other types ...
// here I know for sure that item is of [String:String] type
(item as? [String:String])?["name"] = "orange"
// error: '@lvalue $T6' is not identical to '(String, String)'
}
var fruits = ["name": "apple", "color": "red"]
foo(fruits)