Cody Weaver
Cody Weaver

Reputation: 4886

Dynamic properties in Realm

I am starting to work with Realm on iOS 8 or greater and looking at the documentation in Realm. I noticed that all of the properties have the dynamic keyword in front of them. Is that required in Realm? I have read the Apple documentation on the keyword which can be found here. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html

Upvotes: 25

Views: 8318

Answers (2)

Khawar Islam
Khawar Islam

Reputation: 2914

In Swift 3, we declared our property like this

dynamic var Name : String = ""

In Swift 4, we declared our property like this

@objc dynamic var Name : String = ""

Upvotes: 1

Edman
Edman

Reputation: 5605

Yes, it is mandatory for normal var properties. From the realm docs.

Realm model properties need the dynamic var attribute in order for these properties to become accessors for the underlying database data.

There are two exceptions to this: 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.

The dynamic keyword is what allows for Realm to be notified of changes to model variables, and consequently reflect them to the database.

Upvotes: 39

Related Questions