Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom transient property in Codable struct

I have following Codable struct that working as expected

struct VideoAlbum: Codable {


 let id, image: String?
 let video, mediaType: JSONNull?
 let type, deleted, createdOn: String?
 let modifiedOn: JSONNull?

  enum CodingKeys: String, CodingKey {
    case id, image, video
    case mediaType = "media_type"
    case type, deleted
    case createdOn = "created_on"
    case modifiedOn = "modified_on"
 }

}

// MARK: Encode/decode helpers

class JSONNull: Codable {
public init() {}

public required init(from decoder: Decoder) throws {
    let container = try decoder.singleValueContainer()
    if !container.decodeNil() {
        throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
    }
}

public func encode(to encoder: Encoder) throws {
    var container = encoder.singleValueContainer()
    try container.encodeNil()
}
}

Now I need custom property to be added that are not coming from API to track video position so I modified it

struct VideoAlbum: Codable {
    
    let id, image: String?
    let video, mediaType: JSONNull?
    let type, deleted, createdOn: String?
    let modifiedOn: JSONNull?
    
    var isPlaying:Bool? // CUSOTM PROPERTY 
    var currentTime:CMTime? // CUSOTM PROPERTY 
    var timeObserver:Any? // CUSOTM PROPERTY  
    var pausedByUser:Bool? // CUSOTM PROPERTY 
    
    enum CodingKeys: String, CodingKey {
        case id, image, video
        case mediaType = "media_type"
        case type, deleted
        case createdOn = "created_on"
        case modifiedOn = "modified_on"

        case isPlaying,pausedByUser
        case currentTime
        case timeObserver
    }
}

However it is showing

error Type 'VideoAlbum' does not conform to protocol 'Decodable'

Is there any way to not use some property as Codable ?

I know issue is with CMTime and Any I don't know how to fix it

I have search many question but there is all property are from API, not found for custom property Any one suggest me any solution or alternate way ?

like image 661
Prashant Tukadiya Avatar asked Mar 27 '18 06:03

Prashant Tukadiya


1 Answers

If you don't want to decode those 4 properties, just don't include them in the CodingKeys (and don't forget to explicitly provide default values for them, so that the decoder can initialize the object properly):

struct VideoAlbum: Codable {

    let id, image: String?
    let video, mediaType: JSONNull?
    let type, deleted, createdOn: String?
    let modifiedOn: JSONNull?

    var isPlaying: Bool? = nil
    var currentTime: CMTime? = nil
    var timeObserver: Any? = nil
    var pausedByUser: Bool? = nil

    enum CodingKeys: String, CodingKey {
        // include only those that you want to decode/encode
        case id, image, video
        case mediaType = "media_type"
        case type, deleted
        case createdOn = "created_on"
        case modifiedOn = "modified_on"
    }
}
like image 169
Milan Nosáľ Avatar answered Nov 09 '22 20:11

Milan Nosáľ