How to store an array of strings in a Realm instance using a dictionary?

I am new to Realm and have this problem.

I have Dictionarylike this

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

and a classlike this

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

While I try to insert the values, it throws an exception as shown below

The nickNames property is declared as an NSArray, which is not a supported type of the RLMObject property. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjectsor subclasses RLMObject.
See https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for details .
I also tried

var nickNames =  NSArray()

var nickNames =  NSMutableArray()

But does not work. Do I need to create a Nickname model class and create a property as follows, or is there a way to do this?

var nickNames =  List<Nickname>()
+4
3

UPDATE:

( : , , , ) RLMArrays Lists. , -. .

, , , 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

, Realm, .

+1

Realm , NSArray s, List (, List ). Nickname, , List<Nickname>, .

GitHub , 2014 . , , .

( , let, var.)

0

Using List is the only way to do this. When you initialize the Nickname object (the scope object that you created for use in the List), you must provide an array for the param parameter, even if this value is actually just one row. For instance:

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

That's why he threw an error saying "Invalid value" John "to initialize an object of type" Alias ​​".

0
source

Source: https://habr.com/ru/post/1670377/


All Articles