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
}

推荐答案

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!

Swift相关问答推荐

如何在 SwiftUI 中正确实现Collection 夹?

调整文本的字体大小以适应 UIButton

如何在 Swift 中将 UILabel 居中?

你如何在 UIBarItem 中使用 setTitleTextAttributes:forState?

如何从一个可观察的数组创建一个可观察的数组?

如何在 SwiftUI 中以编程方式滚动列表?

如何使用 Swift 枚举作为字典键? (符合 Equatable)

使用 swift IOS 使 UIBarButtonItem 消失

如何快速从另一个视图控制器重新加载表格视图

NSLocationWhenInUseUsageDescription 警告,但我已经添加了

具有容量的 Swift 初始化数组

Swift 中的“非名义类型”是什么?

WKWebView 内容加载函数永远不会被调用

是否可以向 Swift 协议一致性扩展添加类型约束?

在 Swift 的 MKMapView 中显示当前位置和更新位置

当应用程序处于前台 Swift 3 时获取本地通知

在Xcode导航栏控制器的标题下添加字幕

以毫秒为单位的 Swift 完整日期

不使用数组获取字符串的最后一个字符?

如何使用 Swift 打开谷歌 map 以显示路由