Swift 泛型初始化 : Non-nominal type 'T' does not support explicit initialization

标签 swift generics

我在我的 iOS 应用程序中使用 Realm,由于线程安全问题,我选择为我的数据模型创建一个领域层,以及一个可以与领域对象相互转换的层。

我实际做的事情要复杂得多,但我能够创建一个 playground 来演示我遇到的意外问题。

基本上我创建了一个名为 RealmObject 的协议(protocol)。在我的实际应用程序中,这是一个 Realm.Object 类型可以遵守的协议(protocol),它需要所有必要的属性。

我只是在 playground 中包含此协议(protocol)的 UUID 部分。

//the realm layer
protocol RealmObject {
    var uuid: String { get }
}

接下来我有我的领域层模型。它们都有 UUID,但还有一些其他属性对应于它们的非 Realm 亲属。

struct NoteObject: RealmObject {
    let uuid: String
    let noteText: String
}
struct AppointmentObject: RealmObject {
    let uuid: String
    let time: Date
}

Cacheable 是可以保存在realm 中的类型必须遵守的协议(protocol)。 现在我只包含了 UUID 要求,以及从 RealmObject 初始化的要求

//Cacheable
protocol Cacheable {
    associatedtype T: RealmObject
    init(object: T)
}
struct Note: Cacheable {

    let noteText: String
    let uuid: String
    init(object: NoteObject) {
        self.uuid = object.uuid
        self.noteText = object.noteText
    }
}
struct Appointment: Cacheable {
    let uuid: String
    let time: Date
    init(object: AppointmentObject) {
        self.uuid = object.uuid
        self.time = object.time
    }

}

终于是问题了。在我的应用程序中,此功能的作用远不止于此,但这是我能做到的最简单的事情,并且仍然可以证明该问题。 基本上,T 是泛型类型,它必须是一个可缓存对象。 U 是我要转换为可缓存对象的 RealmObject。

每个可缓存对象都有一个初始化器,它接受一个 RealmObject 对象

但是失败了

func getCacheable<T: Cacheable, U: RealmObject>(from realmObject: U) -> T {

    let thing = T(object: realmObject)
    ERROR: Non-nominal type 'T' does not support explicit initialization
    return thing
}

let noteObject = NoteObject(uuid: "bobalobla", noteText: "hi how are you")
let note: Note = getCacheable(from: noteObject)

let appointmentObject = AppointmentObject(uuid: "bobloblaw", time: Date())
let appointment: Appointment = getCacheable(from: appointmentObject)

我没有看到任何编译器不应该能够轻易弄清楚的模棱两可的东西

用类型替换泛型应该很简单

一旦函数知道它使用的是哪个 Cacheable 类型,初始化程序应该很容易路由到正确的初始化方法。我不会假装理解实际发生的事情,但这似乎是与泛型相关的非常基本的事情,所以我认为我一定是做错了什么。这是怎么回事?

最佳答案

类型系统不知道,从你的泛型列表<T: Cacheable, U: RealmObject> , 那URealmObject 的正确类型对于 T (换句话说,您可以将 Note 作为 T 传递,将 AppointmentObject 作为 U 传递)。您只需将函数签名更新为:

// The `where T.T == U` is the important part. Because that is defined,
// we can also remove `U: RealmObject` because it's redundant information.
func getCacheable<T: Cacheable, U>(from realmObject: U) -> T where T.T == U {
    let thing = T(object: realmObject)
    return thing
}

// Alternatively, you can just do:
func getCacheable<T: Cacheable>(from realmObject: T.T) -> T {
    let thing = T(object: realmObject)
    return thing
}

关于Swift 泛型初始化 : Non-nominal type 'T' does not support explicit initialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49782890/

相关文章:

ios - 同一单元格中 TextView 和 ImageView 的动态单元格大小

swift - 我如何从 uiviewcontroller 转到 TabBarController

delphi - E2511 类型参数 'T' 必须是类类型编译器错误是什么意思?

java - 奇怪的错误,试图在 java 中创建一个通用链表类

java - List<Dog> 是 List<Animal> 的子类吗?为什么 Java 泛型不是隐式多态的?

Ios 开发 : How often should an app check user is still logged in?

ios - Swift + GIDSignIn - hasAuthInKeyChain 返回 true 但 currentUser 在调用 signInSilently 后仍然为 nil

java - 无界通配符类型仅用于创建引用吗?

c# - 是否可以从委托(delegate)中别名/引用委托(delegate)?

ios - 如何将 UIExtendedSRGBColorSpace 与我的颜色值匹配