13

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 ?

Community
  • 1
  • 1
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • What do you mean by `custom` property? Are they your custom `Type` or just some properties that aren't in your JSON response? – nayem Mar 27 '18 at 06:42
  • @nayem Some property not in json response and which type is anything like Class or Any , or Struct – Prashant Tukadiya Mar 27 '18 at 06:54

2 Answers2

19

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"
    }
}
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

First change from type struct to class. Add a parent class that doesn't conform to Codable protocol, for example VideoAlbumStatus and add those custom properties. Now just inherit VideoAlbum from parent class.

class VideoAlbumStatus {
    var isPlaying:Bool? // CUSOTM PROPERTY 
    var currentTime:CMTime? // CUSOTM PROPERTY 
    var timeObserver:Any? // CUSOTM PROPERTY  
    var pausedByUser:Bool? // CUSOTM PROPERTY 
}

class VideoAlbum: VideoAlbumStatus, 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"
    }

    //TO DO 
    // init() for VideoAlbum class

}
susanna
  • 11
  • 5