在对象子类及其自己的子类上都实现ignoreProperties() [英] Implementing ignoredProperties() on both a Object subclass and its own subclass

查看:116
本文介绍了在对象子类及其自己的子类上都实现ignoreProperties()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Realm的新手.我正在使用继承自Object的基类以及该基类的自定义子类来创建模型.我的模型要求基类通过覆盖静态ignoredProperties()方法来声明一些被忽略的属性.当尝试在某些基类子类上重写该方法时,出现了Swift编译器错误,指出 Class方法重写了"final"类方法.我没有将我的基类实现标记为final.我不知道这是否是Realm的当前限制,但我似乎找不到与此问题有关的任何参考.

I'm new to Realm. I'm creating my models with a base class that inherits from Object, and custom subclasses of this base class. My model requires that the base class declares some properties as ignored by overriding the static ignoredProperties() method. When trying to override that method on some of the base class subclasses, I get a Swift compiler error stating that Class method overrides a 'final' class method. I don't have my base class implementation marked as final. I don't know if this is a current limitation with Realm but I can't seem to find any references to this issue.

我的代码如下:

class Base: Object {
    // properties declarations

    override static func ignoredProperties() -> [String] {
        return ["someProperty"]
    }
}

class SomeModel: Base {
    // properties declarations

    // compiler error here
    override static func ignoredProperties() -> [String] {
        var ignoredProperties = super.ignoredProperties()
        ignoredProperties.append("someOtherProperty")
        return ignoredProperties
    }
}

有什么想法或建议吗?我目前正在使用CocoaPods的最新Realm,当前的Xcode(7.2.1)和最新的Swift.

Any ideas or suggestions? I'm currently using the latest Realm from CocoaPods, on current Xcode (7.2.1) and latest Swift.

推荐答案

您已将ignoredProperties声明为:

override static func ignoredProperties() -> [String]

应该是:

override class func ignoredProperties() -> [String]

static函数不能被子类覆盖. class函数可以.

static functions cannot be overridden by subclasses. class functions can.

这篇关于在对象子类及其自己的子类上都实现ignoreProperties()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆