Suresh
Suresh

Reputation: 1259

Realm - Property cannot be marked dynamic because its type cannot be represented in Objective-C

I am trying to implement below scenario, but i am facing the issue

class CommentsModel: Object {
  dynamic var commentId = ""
  dynamic var ownerId: UserModel?
  dynamic var treeLevel = 0
  dynamic var message = ""
  dynamic var modifiedTs = NSDate()
  dynamic var createdTs = NSDate()

 //facing issue here 
 dynamic var childComments = List<CommentsModel>()
}

I have a comments model which has non optional properties in which childComments is List of same Comments model class. In this when i declare dynamic var childComments = List<CommentsModel>()

it shows me Property cannot be marked dynamic because its type cannot be represented in Objective-C.

Please help me how to achieve my requirement

Upvotes: 12

Views: 11880

Answers (2)

chainstair
chainstair

Reputation: 817

Just to make it more understandable how you can add data to the list although its declared as let.

import Foundation
import RealmSwift
import Realm

class Goal: Object {
    //List that holds all the Events of this goal
    let listOfEvents = List<CalEvent>()

    required public convenience init(eventList: List<CalEvent>) {
        self.init()
        for event in eventList {
            //Append the date here
            self.listOfEvents.append(i)
        }
    }
}

Upvotes: 2

Dmitry
Dmitry

Reputation: 7340

List and RealmOptional properties cannot be declared as dynamic because generic properties cannot be represented in the Objective‑C runtime, which is used for dynamic dispatch of dynamic properties, and should always be declared with let.

Learn more in Docs.

So you should declare childComments this way:

let childComments = List<CommentsModel>()

Upvotes: 13

Related Questions