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

时间:2016-12-02 05:08:27

标签: swift xcode 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

2 个答案:

答案 0 :(得分:10)

  

List和RealmOptional属性不能声明为动态,因为泛型属性不能在Objective-C运行时表示,它用于动态调度动态属性,并且应始终使用let声明。

Docs了解详情。

所以你应该用这种方式声明childComments

let childComments = List<CommentsModel>()

答案 1 :(得分:0)

只是为了使您更容易理解如何将数据添加到列表中,尽管其声明为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)
        }
    }
}