gpt4 book ai didi

iOS Swift 可解码 : Error: Cannot invoke initializer for type with no arguments

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:40 24 4
gpt4 key购买 nike

我在初始化结构时遇到错误,请参阅下面的屏幕截图。调试后我发现在结构中包含 review 变量会产生问题。我不知道我做错了什么。谁能帮帮我?

发送

我正在复制代码以防你需要尝试一下

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()
}
}

enter image description here

最佳答案

评论没有默认值,你需要改变这个

var profileValue = ProfileValue()

var profileValue:ProfileValue?

var review: ReviewValues?

ProfileValue结构中提供init方法

关于iOS Swift 可解码 : Error: Cannot invoke initializer for type with no arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com