简体   繁体   English

Swift class inheritance 初始化程序错误

[英]Swift class inheritance initializer error

What do I need to add to extend the UserModel with AuthenticatedUserModel without getting this error?我需要添加什么才能使用 AuthenticatedUserModel 扩展 UserModel 而不会出现此错误? // 'required' initializer 'init(from:)' must be provided by subclass of 'UserModel'

I know I can also just add the accessToken to the UserModel as optional but I want to understand what's going on so I can understand swift a bit better?我知道我也可以将 accessToken 作为可选添加到 UserModel,但我想了解发生了什么,以便更好地理解 swift?

class UserModel: Codable, Identifiable, ObservableObject {
    let id: Int
    let firstName: String?
    let lastName: String?
    let username: String?
    let bio: String?
    let theme: String?
    let imageSrc: String?
    let interests: [String]?
    let followerCount: Int?
    let following: Bool?
    let followingCount: Int?
    let hasCompletedRegistration: Bool?
    let isPrivate: Bool?
    let readerMode: Bool?
    let isActive: Bool?
    let isVerified: Bool?
    let isSuspended: Bool?
    let isAdmin: Bool?
    let isFollowing: Bool?
    let createdAt: String?
    let updatedAt: String?
    
    init(id: Int, firstName: String, lastName: String, bio: String) {
        self.id = id
        self.firstName = firstName
        self.lastName = lastName
        
        self.username = ""
        self.bio = ""
        self.theme = ""
        self.imageSrc = ""
        self.interests = [""]
        self.followerCount = 0
        self.following = false
        self.followingCount = 0
        self.hasCompletedRegistration = true
        self.isPrivate = false
        self.readerMode = true
        self.isActive = true
        self.isVerified = false
        self.isSuspended = false
        self.isAdmin = false
        self.isFollowing = false
        self.createdAt = ""
        self.updatedAt = ""
    }
    
    var name: String {
        return "\(firstName ?? "") \(lastName ?? "")"
    }
}

class AuthenticatedUserModel: UserModel {
    let accessToken: String?
    
    override init(id: Int, firstName: String, lastName: String, bio: String) {
        self.accessToken = nil
        super.init(id: id, firstName: firstName, lastName: lastName, bio: bio)
    }
}

The Decodable protocol requires the implementation of the method init(from:) . Decodable 协议需要实现方法init(from:) Cause of adding a new property the automatically created init(from:) method of the parent class is not inherited.添加新属性的原因是父 class 的自动创建的init(from:)方法未被继承。 This happens cause the inherited method can not initialize the new property from the child class.发生这种情况的原因是继承的方法无法从子 class 初始化新属性。

Therefore the solution is to add the required method.因此解决方案是添加所需的方法。 For example like this (untested code)例如像这样(未经测试的代码)

required init(from decoder: Decoder) throws {
    accessToken = try decoder.singleValueContainer().decode(String.self)
    try super.init(from: decoder)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM