Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm: Swift `let` property cannot be marked as dynamic

Tags:

swift

realm

I am using Xcode 7.2, Swift 2.1.1. I have a Realm model object below

class B: Object {
    dynamic let lists = List<A>() 
}

But the Swift compiler gives me an error saying:

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

I saw Realm's documentation that says:

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

But declaring let doesn't seem to solve this case now. What am I missing?

like image 596
tropicalfish Avatar asked Oct 19 '22 17:10

tropicalfish


1 Answers

The documentation you quoted includes the following (emphasis mine):

List and RealmOptional properties cannot be declared as dynamic because generic properties cannot be represented in the Objective-C runtime, […], and should always be declared with let.

This means your property should be declared like so:

let lists = List<A>()

The Realm Swift documentation recently gained a property declaration cheatsheet which hopefully clarifies the requirements for the different types of declarations.

like image 156
bdash Avatar answered Oct 21 '22 16:10

bdash