从初始化程序返回而不初始化所有存储的属性Swift Xcode 10.0

时间:2019-01-22 10:36:08

标签: swift

结构中的所有变量都是可选的,然后在构造函数中,我也遇到此问题? “从初始化初始化器返回而不初始化所有存储的属性”

struct Conversation : Codable {


    let chat_id : String?
    let id : String?
    let name : String?
    let profile_pic : String?
    let last_message_from : String?
    let message : String?
    let time : String?
    let unread_count : String?
    let member_count : String?
    var type : ChatType = .Single
    var doctors:[Doctors]?

    enum CodingKeys: String, CodingKey {
        case chat_id = "chat_id"
        case id = "id"
        case name = "name"
        case profile_pic = "profile_pic"
        case last_message_from = "last_message_from"
        case message = "message"
        case time = "time"
        case unread_count = "unread_count"
        case member_count = "member_count"
        case doctors = "doctors"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        chat_id = try values.decodeIfPresent(String.self, forKey: .chat_id)
        id = try values.decodeIfPresent(String.self, forKey: .id)
        name = try values.decodeIfPresent(String.self, forKey: .name)
        profile_pic = try values.decodeIfPresent(String.self, forKey: .profile_pic)
        last_message_from = try values.decodeIfPresent(String.self, forKey: .last_message_from)
        message = try values.decodeIfPresent(String.self, forKey: .message)
        time = try values.decodeIfPresent(String.self, forKey: .time)
        unread_count = try values.decodeIfPresent(String.self, forKey: .unread_count)
        member_count = try values.decodeIfPresent(String.self, forKey: .member_count)
        doctors = try values.decodeIfPresent([Doctors].self, forKey: .doctors)
    }



    init(doctor:Doctors) {
        self.id = doctor.doctorId
        self.profile_pic = doctor.doctorPic
        self.type = .Single
    }




}

1 个答案:

答案 0 :(得分:1)

如果创建初始化程序,则需要为初始化程序中所有存储的属性指定值,而不能使用属性的默认值。因此,即使您将属性声明为Optional,如果希望它们为nil,也需要在初始化器中为其分配nil值。

与您的问题无关,但是如果所有属性名称都与JSON密钥匹配,则无需声明CodingKeys,也无需手动编写init(from:)初始化程序,编译器可以自动合成在您的简单情况下为您。但是,您应该遵循Swift命名约定,即变量名(包括enum大小写)是lowerCamelCase,因此要相应地重命名属性,然后需要CodingKeys

请注意,您的许多类型实际上没有任何意义。为什么将变量称为count String?如果它们从后端以String的形式出现,请在Int中将它们转换为init(from:)。另外,在您的init(doctor:)中,将doctor实际添加到doctors数组中是有道理的。

struct Conversation : Codable {

    let chatId: String?
    let id: String?
    let name: String?
    let profilePic: String?
    let lastMessageFrom: String?
    let message: String?
    let time: String?
    let unreadCount: String?
    let memberCount: String?
    var type: ChatType = .single
    var doctors:[Doctors]?

    enum CodingKeys: String, CodingKey {
        case chatId = "chat_id"
        case id
        case name
        case profilePic = "profile_pic"
        case lastMessageFrom = "last_message_from"
        case message
        case time
        case unreadCount = "unread_count"
        case memberCount = "member_count"
        case doctors
    }

    init(doctor:Doctors) {
        self.id = doctor.doctorId
        self.profilePic = doctor.doctorPic
        self.type = .single
        self.chatId = nil
        self.name = nil
        self.lastMessageFrom = nil
        self.message = nil
        self.time = nil
        self.unreadCount = nil
        self.memberCount = nil
        self.doctors = [doctor]
    }
}