Realm - a property cannot be marked as dynamic because its type cannot be represented in Objective-C

I am trying to implement the below script, but I ran into a problem

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 comment model that has optional properties in which childComments is a list of the same comment model classes. In this case, when I declare dynamic var childComments = List<CommentsModel>()

indicates that a property cannot be marked as dynamic because its type cannot be represented in Objective-C.

Please help me how to reach my requirement

+10
source share
2 answers

List and RealmOptional properties cannot be declared dynamic, because common properties cannot be represented in the Objective-C runtime, which is used to dynamically dispatch dynamic properties and must always be declared with let.

More details in Docs .

So you should declare childComments as follows:

 let childComments = List<CommentsModel>() 
+10
source

Just to make it clearer how you can add data to the list, although it is 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) } } } 
0
source

Source: https://habr.com/ru/post/1260683/


All Articles