简体   繁体   English

Realm 与 Swift 5 Inverse 关系在启动时使应用程序崩溃

[英]Realm with Swift 5 Inverse relationship crashes the app on launch

I am learning how to use Realm db, as per examples I'm using the following tasks list with 2 classes: Category and Item, and each category may have many items but each item belong to one category as follows:我正在学习如何使用 Realm db,根据示例,我使用以下任务列表和 2 个类:类别和项目,每个类别可能有许多项目,但每个项目属于一个类别,如下所示:

Category class类别类

import Foundation
import RealmSwift

final class Category: Object {
    @objc dynamic var name : String = ""
    let items = List<Item>()
}

Item class物品类别

import Foundation
import RealmSwift

final class Item: Object {
    @objc dynamic var title: String = ""
    @objc dynamic var done : Bool = false
    let parentCategory = LinkingObjects(fromType: Category.self, property: "Items")
}

when I run the project, it crashes with the following error:当我运行该项目时,它崩溃并出现以下错误:

Fatal error: 'try!'致命错误:“尝试!” expression unexpectedly raised an error: Error Domain=io.realm Code=1 "Schema validation failed due to the following errors:表达式意外引发错误:Error Domain=io.realm Code=1“由于以下错误,架构验证失败:

  • Property 'Category.Items' declared as origin of linking objects property 'Item.parentCategory' does not exist" UserInfo={NSLocalizedDescription=Schema validation failed due to the following errors:属性 'Category.Items' 声明为链接对象的源属性 'Item.parentCategory' 不存在" UserInfo={NSLocalizedDescription=由于以下错误,架构验证失败:
  • Property 'Category.Items' declared as origin of linking objects property 'Item.parentCategory' does not exist, Error Code=1}属性“Category.Items”声明为链接对象的来源属性“Item.parentCategory”不存在,错误代码=1}

General info:基本信息:

Xcode version: 11.6 Xcode 版本:11.6

Realm version: 5.4.0领域版本:5.4.0

OS version: Catalina 10.15.3操作系统版本:Catalina 10.15.3

iOS version : 13 iOS 版本:13

I tried cleaning the project, restarting Xcode and Mac itself deleting realm file itself with no luck.我尝试清理项目,重新启动 Xcode 和 Mac 本身删除领域文件本身没有运气。

I searched stackoverflow questions, many tutorials and above all Realm's documentation , this code should run fine.我搜索了 stackoverflow 问题、许多教程以及最重要的Realm 文档,这段代码应该可以正常运行。

You've just got a typo in the inverse relationship let parentCategory property.您刚刚在逆关系let parentCategory属性中输入了一个错字。 "Items" should be "items" to match the property in the Category class “项目”应该是“项目”以匹配Category类中的属性

final class Item: Object {
    @objc dynamic var title: String = ""
    @objc dynamic var done : Bool = false
    let parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM