arrays - 上下文类型 'Any' 不能与字典文字 Swift 一起使用

标签 arrays swift

我在两个文件中使用此代码,但只有一个文件显示此错误。

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]

enter image description here enter image description here

最佳答案

该错误具有误导性。实际错误是

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/

相关文章:

c++ - C++中定义一个行数未知的数组

javascript - 很难理解回调函数

ios - 如何使用 queryEqual(toValue :childKey:) in Firebase SDK for iOS properly?

ios - UITableViewCell 中的 Swift KingFisher 来自 Url 的图像不是动态的

swift - 除了 Xcode,还有其他地方可以看到我的应用程序崩溃吗?

java - 洗牌 ArrayList

javascript - 使用 lambda 函数在包含对象数组的对象数组中查找 JavaScript 对象

iOS:后退按钮标签被自定义字体截断

php - 在php中处理多种类型的数组

swift - UIView 不从 xib 文件 Swift 调整大小