Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

swift

realm

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

like image 593
Suresh Avatar asked Dec 02 '16 05:12

Suresh


2 Answers

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)
        }
    }
}
like image 163
chainstair Avatar answered Nov 15 '22 23:11

chainstair


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>()
like image 30
Dmitry Avatar answered Nov 16 '22 00:11

Dmitry