我在两个文件中使用此代码,但只有一个文件显示此错误。
Contextual type 'Any' cannot be used with dictionary literal
为什么?这2个代码是一样的,为什么只有一个出错了?这段代码有什么问题?
let userId = user!["userId"] as! String
let bookCid = NSUUID().uuidString
let param = ["message":"addAccBook",
"accountbook":
[
"bookcid": bookCid!,
"accbookname": "",
"accbooktype": "",
"category": "",
"user": userId
]] as [String:Any]
最佳答案
该错误具有误导性。实际错误是
Cannot force unwrap value of non-optional type 'String'
uuidString
返回一个非可选的 String
,因此您不能添加感叹号
let userId = user!["userId"] as! String
let bookCid = NSUUID().uuidString
let param : [String:Any] = ["message":"addAccBook",
"accountbook":
["bookcid": bookCid,
"accbookname": "",
"accbooktype": "",
"category": "",
"user": userId]]
我建议不要桥接类型来注释它。
关于arrays - 上下文类型 'Any' 不能与字典文字 Swift 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50055937/