Start a new topic

How to use List type with Codable? (RealmSwift)

List type does not conform to Codable(Decodable), it is impossible to use Codable if the class has List type.

How to resolve this issue?

class Libraries: Entity, Codable {
    
    @objc dynamic var name: String?
    @objc dynamic var location: GeoPoint?
    let booksList = List<Books>()
    
    override class func collectionName() -> String {
        return "Libraries"
    }
  
    //Map properties in your backend collection to the members of this entity
    enum CodingKeys : String, CodingKey {
        case name = "name"
        case location = "location"
        case booksList = "books"
    }
    
    // Swift.Decodable
    required init(from decoder: Decoder) throws {
        //This maps the "_id", "_kmd" and "_acl" properties
        try super.init(from: decoder)
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decodeIfPresent(String.self, forKey: .name)
        location = try container.decodeIfPresent(GeoPoint.self, forKey: CodingKeys.location)
        let booksArray = try container.decodeIfPresent([Books].self, forKey: CodingKeys.booksList) 
        booksList.append(objectsIn: booksArray)
        
    }


This solution doesn't work for me (Ambiguous reference to member 'decodeIfPresent(_:forKey:)')

  let booksArray = try container.decodeIfPresent([Books].self, forKey: CodingKeys.booksList) 

        booksList.append(objectsIn: booksArray)   


Could u please help me?

1 Comment

Thanks I found mistake

Login or Signup to post a comment