无论我使用哪种类型,都能获得"Type Mismatch"



我正在尝试使用 Codable 从后端解析一些 JSON 响应,但总是得到类型不匹配。

我以前从未在Codable上遇到过这个问题。

这是我的 JSON:

{
"success": true,
"result": {
"uuid": "ed26b73f-9661-44e0-9975-d841bc533849",
"update_time": "1568035713",
"category_uuid": "b36c1549-c4c9-11e9-8fcd-9151e37ecd87",
"title": "optionaltask",
"description": "",
"reminder_time": 1212121212,
"same_day": 0,
"before_day": 0,
"three_days": 0,
"by_email": 0,
"by_sms": 0,
"by_notification": 0,
"mp3_time": null,
"file_name": null,
"file_link": null,
"status": "0"
},
"error": null
}

这是我的可编码结构:


struct RespElement: Codable {
let success: Bool
let result: Task
let error: String?
}
class Task: Object, Codable {
var uuid, updateTime, categoryUUID, title: String
var resultDescription: String?
var sameDay, beforeDay, threeDays: Int
var bySMS, byNotification: Int
var byEmail: Int
var reminderTime: Int
var mp3Time: Int?
var fileName, fileLink: String?
var status: String
enum CodingKeys: String, CodingKey {
case uuid
case updateTime = "update_time"
case categoryUUID = "category_uuid"
case title
case resultDescription = "description"
case reminderTime = "reminder_time"
case sameDay = "same_day"
case beforeDay = "before_day"
case threeDays = "three_days"
case byEmail = "by_email"
case bySMS = "by_sms"
case byNotification = "by_notification"
case mp3Time = "mp3_time"
case fileName = "file_name"
case fileLink = "file_link"
case status
}

我希望它被正确解码,但我得到了一个奇怪的行为。 解码器显示 Int 变量的错误,表示预期的 Int 但找到了一个字符串/数据。当我将变量类型更改为字符串时,它说预期的字符串,但找到了一个数字。当我尝试将其解析为 Bool 时,它也会抛出类型不匹配。

你的结构应该是这样的:

class Task: Codable { var success: Bool var result: [Result] var error: String? }

class Result: Codable
{
var sameDay: Int
var beforeDay: Int
var threeDays: Int
var by_email: Int
var by_sms: Int
var by_notification: Int

}

试试这个

相关内容

  • 没有找到相关文章

最新更新