无法将“字符串”类型的值转换为预期的参数类型“ AnyObject?”

时间:2020-10-07 12:53:49

标签: ios swift

我有以下代码

let bookId = dict["bookId"] as? String ?? ""
    let date = dict["date"] as? String ?? ""
    let xp = dict["xp"] as? Double ?? 0.0

if (bookId !== "summary") {
    hasBooks[bookId] = true
    numCompletedBooks += 1
    totalXp += 10
    if dateNumBooks[date] == nil {
        dateNumBooks[date] = 1
    } else {
        dateNumBooks[date]! += 1
    }
    dateBooks[bookId] = date
} else {
    streak = dict["streak"] as? Int ?? 0
}

在行Cannot convert value of type 'String' to expected argument type 'AnyObject?'上出现以下错误if (bookId !== "summary") {

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

替换

if (bookId !== "summary")

使用

if bookId != "summary"

!=而不是!==检查What does !== mean in Swift?