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


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!

Leave a Reply