【发布时间】:2018-11-30 04:29:21
【问题描述】:
我在初始化结构时遇到错误,请参阅下面的屏幕截图。调试后,我发现在结构中包含审查变量会出现问题。 我无法弄清楚我做错了什么。 谁能帮帮我?
发送
我正在复制代码以防万一您需要尝试一下
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()
}
}
【问题讨论】:
-
我将您的代码扔到 Playground 中,并被要求使用
var profileValue = ProfileValue(id: 0, name: "", review: ReviewValues(place: ""))。要克服它,您必须提供自定义init函数,但review不是可选的,因此您必须为其提供一个值
标签: ios swift swift4 decodable