Home IOS Development ios – Why my structs would not affirm to encodable and decodable?

ios – Why my structs would not affirm to encodable and decodable?

0
ios – Why my structs would not affirm to encodable and decodable?

[ad_1]

Listed here are my structs for decoding JSON recordsdata. Xcode provides errors and says they do not affirm encodable and decodable. This one :

    struct SocialModel: Codable {
    let id : Int
    let likeCount: Int
    let commentCounts: CommentCounts
    
    enum CodingKeys: String, CodingKey {
        case id
        case likeCount
        case commentCounts
    }
    
    init(id: Int, likeCount: Int, commentCounts: CommentCounts) {
        self.id = id
        self.likeCount = likeCount
        self.commentCounts = commentCounts
    }
    
    init(from decoder: Decoder) throws {
        let container = strive decoder.container(keyedBy: CodingKeys.self)
        id = strive container.decodeIfPresent(Int.self, forKey: .id) ?? 123
        likeCount = strive container.decode(Int.self, forKey: .likeCount)
        commentCounts = strive container.decode(CommentCounts.self, forKey: .commentCounts)
    }
}

struct CommentCounts: Codable {
    let averageRating, anonymousCommentsCount, memberCommentsCount: Int
}

and this one :


struct ProductModel: Codable {
    let productId: Int
    let identify, desc: String
    let productImage: String
    let productPrice: Value
}

struct Value: Codable {
    let worth: Int
    let forex: String
}

Anybody can inform me why? I attempted construct them many occasions however it did not work at on the finish.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here