Why does Realm use RealmOptional rather than Int? for optional properties?

前端 未结 1 841
离开以前
离开以前 2021-01-13 13:12

Realm\'s documentation on optional properties states:

String, NSDate, and NSData properties can be declared as

1条回答
  •  长发绾君心
    2021-01-13 14:02

    Realm model classes automatically implement getters and setters for your persisted properties that access the underlying database data. In order to provide these getters and setters, your properties must be declared with the dynamic modifier. This modifier asks Swift to dynamically dispatch accesses to the properties via getters and setters rather than directly accessing the member at compile time. The dynamic modifier comes with a significant limitation: it is only supported for types that can be represented in Objective-C. This is because Swift's dynamic dispatch is built on top of the Objective-C runtime. It is this limitation that prevents Int? from being directly supported by Realm.

    You may wonder how String?, NSData?, and NSDate? are supported given this limitation. The answer is that they have natural equivalents in Objective-C, namely nullable NSString *, nullable NSData *, and nullable NSDate *. No such equivalents exist for Swift's numeric types.

    0 讨论(0)
提交回复
热议问题