Mat*_*att 6 ios swift swift4 decodable
初始化结构时出现错误,请参见下面的屏幕截图。调试后,我发现在结构中包含review变量会产生问题。我不知道我在做什么错。谁能帮我吗?
Tx
我正在复制代码,以防万一您需要尝试一下
import UIKit
struct RootValue : Decodable {
private enum CodingKeys : String, CodingKey {
case success = "success"
case content = "data"
case errors = "errors"
}
let success: Bool
let content : [ProfileValue]
let errors: [String]
}
struct ProfileValue : Decodable {
private enum CodingKeys : String, CodingKey {
case id = "id"
case name = "name"
case review = "review" // including this gives error
}
var id: Int = 0
var name: String = ""
var review: ReviewValues // including this gives error
}
struct ReviewValues : Decodable{
private enum CodingKeys : String, CodingKey {
case place = "place"
}
var place: String = ""
}
class ViewController: UIViewController {
var profileValue = ProfileValue()
override func viewDidLoad() {
super.viewDidLoad()
}
}
Run Code Online (Sandbox Code Playgroud)
评论没有默认值,您需要更改此值
var profileValue = ProfileValue()
Run Code Online (Sandbox Code Playgroud)
至
var profileValue:ProfileValue?
Run Code Online (Sandbox Code Playgroud)
要么
var review: ReviewValues?
Run Code Online (Sandbox Code Playgroud)
要么
结构中的供应init方法ProfileValue