Zack Shapiro Zack Shapiro -4 years ago 108
Swift Question

Storing an array of custom objects in Realm

I have an object called

which subclasses Realm's

class List: Object {
dynamic var brandListItems: [BrandListItem] = []

and another object,
which also subclasses to

class BrandListItem: Object {
dynamic var brandID: String?
dynamic var name: String?

My app is crashing with the following error

'Property 'brandListItems' is declared as 'NSArray', which is not a supported RLMObject property type. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject.

I tried doing something like
with no luck. How do I successfully save these objects to Realm?

Answer Source

You need to use Realm's List<T> property. Note that it's not marked dynamic

class List: Object {
   let brandListItems = List<BrandListItem>()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download