致命错误:在类MyApp.User中使用未实现的初始化初始化器的init(域:模式:)。

时间:2022-09-07 07:54:14

Im getting this error when i try to load a user from the database.

当我试图从数据库中加载用户时,我将得到这个错误。

    dispatch_async(dispatch_queue_create("background", nil)) {
        let realm = try! Realm()
        let users = realm.objects(User)
        print(users)
    }


class User: Object, Mappable {

  dynamic var id = 0
  dynamic var name = ""
  dynamic var userName = ""

required init() {
    super.init()
}

// MARK: Mappable
func mapping(map: Map) {
    id <- map["Id"]
    name <- map["Name"]
    userName <- map["UserName"]
}

required init?(_ map: Map) { super.init() }

}

}

I tried implemeting that init method but i get (Use of undeclared identifier RLMObjectSchema): 致命错误:在类MyApp.User中使用未实现的初始化初始化器的init(域:模式:)。

我尝试使用init方法,但我得到了(使用未声明的标识符RLMObjectSchema):

Any hints?

有提示吗?

1 个解决方案

#1


1  

When I use realm, I will only use convenience init.

当我使用领域时,我将只使用方便的init。

In your case

在你的情况中

class User: Object, Mappable {

    dynamic var id = 0
    dynamic var name = ""
    dynamic var userName = ""

    // MARK: Mappable
    func mapping(map: Map) {
        id <- map["Id"]
        name <- map["Name"]
        userName <- map["UserName"]
    }

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

If you want implement designate init, you should implement

如果您想要实现指定的init,您应该实现

init(realm: RLMRealm, schema: RLMObjectSchema) {
    super.init(realm: realm, schema: schema)
}

When you implement designate init(), swift will not inherit other designate init methods which are required by realm.

当您实现指定的init()时,swift将不会继承由域所要求的其他指定的init方法。

#1


1  

When I use realm, I will only use convenience init.

当我使用领域时,我将只使用方便的init。

In your case

在你的情况中

class User: Object, Mappable {

    dynamic var id = 0
    dynamic var name = ""
    dynamic var userName = ""

    // MARK: Mappable
    func mapping(map: Map) {
        id <- map["Id"]
        name <- map["Name"]
        userName <- map["UserName"]
    }

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

If you want implement designate init, you should implement

如果您想要实现指定的init,您应该实现

init(realm: RLMRealm, schema: RLMObjectSchema) {
    super.init(realm: realm, schema: schema)
}

When you implement designate init(), swift will not inherit other designate init methods which are required by realm.

当您实现指定的init()时,swift将不会继承由域所要求的其他指定的init方法。