RealmSwift初始化列表:无法专用于非泛型定义 [英] RealmSwift initialize list : Cannot specialize a non-generic definition

查看:49
本文介绍了RealmSwift初始化列表:无法专用于非泛型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异常,例如无法专门化非通用定义"当我尝试初始化Realm对象中的List时.有人知道如何解决此问题吗?迅速3.2

Hi I have an exception like "Cannot specialize a non-generic definition" when i'm trying to initialize List in Realm object. Does anyone know how to fix this issue? swift 3.2

class Dog: Object {
    @objc dynamic var name = ""
    @objc dynamic var age = 0 
}

class Event : Object{
    dynamic var event_id = 0
    dynamic var date : String?
    dynamic var name : String?
    dynamic var remind : Remind?
    dynamic var event_status = 0

    let dogs = List<Dog>()       "Cannot specialize a non-generic definition"

    required convenience init?(map: Map){ self.init() } 
}

推荐答案

我想为这个问题提供一些解决方案.在Swift中,名称间隔是在模块级别上隐含的.如果您有外部模块,例如RealmSwift,其类型为 List ,则您自己的模块中仍可以具有名为 List 的类型.您甚至可以使用类型为 List 的多个外部模块.

I wanted to give the solution to this question some context. In Swift, namespacing is implicit on a module level. If you have external modules, such as RealmSwift, which has a type List you can still have a type named List in your own module. You can even have multiple external modules with the type List.

我对出现的错误的原因如下

My reasoning for the error you presented was as follows

  1. 在Realm中,您发布的内容完全是您应该如何声明列表
  2. 您的示例是在我的项目的上下文中编译的,该项目已使用Realm List s
  3. 所指示的错误表明,快速编译器知道 List 是有效类型,并且实际上知道名为 List.init()的函数,但是,编译器告诉您该函数不是通用的.
  1. In Realm, what you posted is exactly how you're supposed to declare a list
  2. Your example compiled in the context of my project which already uses Realm Lists
  3. The error indicated suggests that the swift compiler knows that List is a valid type and in fact knows about a function named List.init(), however, the compiler is telling you that the function isn't generic.

此时唯一的答案是,假设编译器使用的 List 定义与您预期的不同.发生这种情况的唯一方法是,如果您所在的范围(您的项目或其他类型)也定义了类型 List ,因为内部类型优先于外部类型.此优先级通过类型嵌套进一步扩展.如果您想将Realm的 List 保留为默认列表,则可以将 List 嵌套在另一个结构中,以提供各种命名空间.

The only answer at that point, is to assume the compiler is using a different definition of List than you are intending. The only way this happens is if the scope you're in (your project or another type), has also defined a type List, since internal types take precedence over external types. This precedence is extended further with type nesting. If you'd like to keep Realm's List as the default list, you can nest your List in another structure to provide a namespace of sorts.

鉴于复杂的情况,以下示例尽可能简洁.

The following example is as concise as possible given the complex situation.

import RealmSwift

let myGlobalDogs = List()
let myGlobalNestedListDogs = MyClass.List()
let globalDogs = RealmSwift.List<Dog>()

class List { }

class MyClass {
    class List { }

    let dogs = RealmSwift.List<Dog>()
    let myNestedListDogs = List() // type: MyModule.MyClass.List
    let myDogs = MyModule.List() // type: MyModule.List
}

class MyOtherClass {

    let dogs = RealmSwift.List<Dog>()
    let myNestedListDogs = MyClass.List() // type: MyModule.MyClass.List
    let myDogs = List() // type: MyModule.List
}

幸运的是,类型/函数通常具有足够的差异,以至于您不会无意中使用错误的类型/功能而不会最终遇到错误,例如遇到的错误.

Fortunately, types/functions are usually different enough that you can't inadvertently use the wrong one without encountering an error eventually, such as the one you encountered.

这篇关于RealmSwift初始化列表:无法专用于非泛型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆