var dict = ["name": "ryan", "age": 13, "gender": "male"]
//error on the next line
dict["hairColor"] = "brown"
在本教程中,我看到上面的代码片段正在运行,但对我来说不是。错误说:
cannot assisgn to the result of this expression.
最佳答案
我可以在旧版本的 Xcode 中重现您的问题,例如 6.0.1。它在最新版本 6.1.1 中运行良好。您应该更新您的 Xcode。
您还需要import Foundation
(或通过import UIKit
或import Cocoa
隐式导入,其中任何一个都将包含Foundation) 使代码工作,因为这允许编译器假定您需要 NSObject
的字典,因为您试图将混合类型(整数和字符串)放入字典中。这个完整的示例在 6.1.1 中运行良好:
import Foundation // Or Cocoa, or UIKit
var dict = ["name": "ryan", "age": 13, "gender": "male"]
dict["hairColor"] = "brown"
关于ios - 为什么不能为字典条目分配新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27598233/