初始化结构时出现错误,请参见下面的屏幕截图。调试后,我发现在结构中包含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()
    }
}

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

最佳答案

评论没有默认值,您需要更改此值

var profileValue = ProfileValue()


var profileValue:ProfileValue?


var review: ReviewValues?



init结构中提供ProfileValue方法

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

10-09 21:05