RealmSwift initialize list : Cannot specialize a non-generic definition

房东的猫 提交于 2021-02-07 20:27:09

问题


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() } 
}

回答1:


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. 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.

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.



来源:https://stackoverflow.com/questions/47336691/realmswift-initialize-list-cannot-specialize-a-non-generic-definition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!