Declare an array of Int in Realm Swift

Vinicius Albino picture Vinicius Albino · Feb 5, 2016 · Viewed 17k times · Source

How can I declare an array of integers inside RLMObject?

Like :

dynamic var key:[Int]?

Gives the following error :

Terminating app due to uncaught exception 'RLMException', reason: ''NSArray' is not supported as an RLMObject property. All properties must be primitives, NSString, NSDate, NSData, RLMArray, or subclasses of RLMObject. See https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for more information.'

Answer

kishikawa katsumi picture kishikawa katsumi · Feb 8, 2016

Lists of primitives are not supported yet unfortunately. There is issue #1120 to track adding support for that. You'll find there some ideas how you can workaround that currently.

The easiest workaround is create a object to hold int values. Then the model to have a List of the object.

class Foo: Object {
    let integerList = List<IntObject>() // Workaround
}

class IntObject: Object {
    dynamic var value = 0
}