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

标签 ios swift swift4 decodable

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

相关文章:

ios - 如何在结构模型中访问枚举? swift

ios - 当步数更新时调用一些东西

swift - SpriteKit : How can I make my HUD following my camera?

objective-c - CoreData 无法完成错误

ios - Xcode 和 Unity 缺少库 'lGoogleUtilities'

ios - 在 iOS 9 的多个 WKWebView 中同步 cookie

ios - 通过 pod 的 MessageKit 会使 iOS 应用程序崩溃

ios - 等待分派(dispatch)异步操作的函数返回(Swift + Parse)

ios - 有没有办法在 React Native for iOS 中获取唯一的设备 ID?

ios - 快速解析非常慢 - 我的编程有问题吗?