11

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>()
Mohshin Shah
  • 298
  • 2
  • 13
  • Realm now supports lists of primitive types directly. See [this answer](https://stackoverflow.com/a/48339013/1315347). – bmunk Jan 19 '18 at 10:36

4 Answers4

10

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.

bmunk
  • 1,488
  • 1
  • 13
  • 22
3

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

0

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.)

AustinZ
  • 1,787
  • 1
  • 13
  • 21
  • Created a model for nick name like this ``` class NickName: Object { dynamic var value: String! } ``` and made the nicknames property as ` let nickNames = List()` It is crashing ``` Invalid value 'John' to initialize object of type 'NickName': missing key 'value'' ``` – Mohshin Shah Feb 22 '17 at 06:44
  • `John` isn't in any of the code snippets you posted in your comment. Can you update your question with the actual code you are using? – AustinZ Feb 22 '17 at 19:27
  • John is the nickname value.. I have just renamed the vars, nothing else.. You can have it `1`. Still updating the question – Mohshin Shah Feb 23 '17 at 06:28
  • Can you try making the `nicknames` array something like `nickNames : [{ value: "John" }, { value: "2" }, { value: "3" } , { value: "4" }]`? (I am assuming your `Nickname` object has a single property named `value`.) – AustinZ Feb 27 '17 at 23:14
  • That is not possible. If I could do that why would I post this question :D :D. I would have changed that when you told. – Mohshin Shah Feb 28 '17 at 07:44
  • I think it does now? – EpicPandaForce Jan 19 '18 at 10:57
0

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'".

Santhosh R
  • 1,518
  • 2
  • 10
  • 14