I have seen this post Optional dynamic properties in Swift but I don't want to have to wrap up the class in an NSObject
. This just is concerning the Realm database I don't have to have nil
properties but it would be a nice way I think to model my database. In the Realm documentation which can be found here https://realm.io/docs/swift/latest/ it says optionals are supported. Here is my
Code
dynamic var complete: Bool? = nil
and here is my
Error
Property cannot be marked dynamic because its type cannot be represented in Objective-C
I know this is the same code and error as the post above but I am just curious if the Realm documentation says it supports it do they have another work around?
From the docs on supported types and optional properties.
String
,NSDate
,NSData
and Object properties can be optional. Storing optional numbers is done usingRealmOptional
.
RealmOptional
supportsInt
,Float
,Double
,Bool
, and all of the sized versions ofInt
(Int8
,Int16
,Int32
,Int64
).
So optionals are supported for String
, NSDate
, NSData
and Object
types nicely with the standard swift syntax.
For other numeric types (such as Bool
) that is done with RealmOptional
. Then to use a variable of this RealmOptional
type you access its value
property, which is an optional that represents your underlying value.
// definition (defined with let)
let complete = RealmOptional<Bool>() // defaults to nil
// usage
complete.value = false // set non-nil value
...
complete.value = nil // set to nil again
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With