xcode 领域 - 无法将属性标记为动态,因为其类型无法在 Objective-C 中表示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40925192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:37:23  来源:igfitidea点击:

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

swiftxcoderealm

提问by Suresh

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>()

我有一个评论模型,它具有非可选属性,其中 childComments 是相同评论模型类的列表。在这当我声明dynamic var childComments = List<CommentsModel>()

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

它显示我不能将属性标记为动态,因为它的类型不能在 Objective-C 中表示。

Please help me how to achieve my requirement

请帮助我如何实现我的要求

回答by Dmitry

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.

List 和 RealmOptional 属性不能被声明为动态的,因为泛型属性不能在 Objective?C 运行时中表示,它用于动态属性的动态调度,并且应该总是用 let 声明。

Learn more in Docs.

Docs 中了解更多信息。

So you should declare childCommentsthis way:

所以你应该这样声明childComments

let childComments = List<CommentsModel>()

回答by chainstair

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

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