Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Persisted String List property RealmSwift

I am trying to set an array of String to a List of String for my Realm Object, but I got and error "Generic type ‘List’ specialized with too few type parameters (got 1, but expected 2)".

I tried List<NSString>, List<String>, List<String>(), I also tried to create my own Object with a value, and I even tried the documentation code and it has the same issue

I tried to put another argument to see which kind of second argument he was waiting for but the error goes to "Generic type ‘List’ specialized with too many type parameters (got 2, but expected 1)" that sounds to be a legit error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


import RealmSwift
import SwiftUI

// Define your models like regular Swift classes
class Dog: Object {
    @Persisted var name: String
    @Persisted var age: Int
}
class Person: Object {
    @Persisted(primaryKey: true) var _id: String
    @Persisted var name: String
    @Persisted var age: Int
    // Create relationships by pointing an Object field to another Class
    @Persisted var dogs: List<Dog> // Error here
}

>Solution :

Since you are importing SwiftUI, the compiler is thinking that you meant the SwiftUI List type, which indeed, has 2 type arguments.

You can refer to the Realm List type by qualifying it:

@Persisted var dogs: RealmSwift.List<Dog>
@Persisted var someStrings: RealmSwift.List<String>

Or better, don’t put your data in the same file as your UI. You should separate them!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading