11
votes

I am new to Realm and having this issue.

I am having a Dictionary like this

{
    firstName : "Mohshin"
    lastName : "Shah"
    nickNames : ["John","2","3","4"]              
}

and a class like this

class User: Object {
    var firstName: String?
    var lastName: String?
    var nickNames: [String]?
}

While I am trying to insert values it is throwing an exception as below

Property 'nickNames' 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.
See https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for more information.
I have also tried

var nickNames =  NSArray()

var nickNames =  NSMutableArray()

But not working.Do I need to make the Nickname model class and create a property as follow or there's a way to do this ?

var nickNames =  List<Nickname>()
4
Realm now supports lists of primitive types directly. See this answer. - bmunk

4 Answers

8
votes

UPDATE:

You can now store primitive types or their nullable counterparts (more specifically: booleans, integer and floating-point number types, strings, dates, and data) directly within RLMArrays or Lists. If you want to define a list of such primitive values you no longer need to define cumbersome single-field wrapper objects. Instead, you can just store the primitive values themselves.

Lists of primitive values work much the same way as lists containing objects, as the example below demonstrates for Swift:

class Student : Object {
    @objc dynamic var name: String = ""
    let testScores = List<Int>()
}

// Retrieve a student.
let realm = try! Realm()
let bob = realm.objects(Student.self).filter("name = 'Bob'").first!

// Give him a few test scores, and then print his average score.
try! realm.write {
    bob.testScores.removeAll()
    bob.testScores.append(94)
    bob.testScores.append(89)
    bob.testScores.append(96)
}
print("\(bob.testScores.average()!)")   // 93.0

All other languages supported by Realm also supports lists of primitive types.

2
votes

you can find more details here. https://academy.realm.io/posts/realm-list-new-superpowers-array-primitives/

class BlogPost: Object {
  @objc var title = ""
  let tags = List<String>()

  convenience init(title: String, tag: String) {
    self.init()
    self.title = title
    self.tags.append(tag)
  }
}

more details here

1
votes

Using List is pretty much the only way to do it. When you initialize the Nickname object (the realm object you created for using in List), you should provide an array for the value param, even if the value is actually just one string. For example:

let aNickName = Nickname(value:["John"])

That is why it was throwing an error saying "Invalid value 'John' to initialize object of type 'Nickname'".

0
votes

Realm doesn't support model properties that are NSArrays, and currently doesn't support properties that are Lists of primitive types (such as Lists of strings). For now, you should create a Nickname model that wraps the nickname string, and then store a List<Nickname>, as in your sample code above.

This ticket on our GitHub repository tracks support for lists of primitives, although none of the comments from 2014 are particularly relevant anymore. You can follow that ticket if you want to be informed as to when that feature will become available.

(Also note that you should declare your list property as let, not var.)