收到以下致命错误。
致命错误:'尝试!'表达式意外地引发了错误:错误 Domain = io.realm Code = 1"由于以下原因,架构验证失败 错误: - Property' GearModel.category'声明为链接对象属性的来源' CategoryRepository.gearList'链接到类型' CategoryModel'" UserInfo = {NSLocalizedDescription =由于以下原因导致架构验证失败 以下错误: - Property' GearModel.category'声明为链接对象属性的来源' CategoryRepository.gearList'链接到类型' CategoryModel', 错误代码= 1}:
此处的对象模型:Gear Model分配给ONE only Category
import Foundation
import RealmSwift
class GearModel: Object{
dynamic var gearID = NSUUID().uuidString
dynamic var name = ""
dynamic var manufacturer = ""
dynamic var sortOrder = 0
dynamic var category: CategoryModel?
override class func primaryKey() -> String? {return "gearID"}
}
类别模型:尝试获取已分配类别的所有GearModel项目的列表。
import Foundation
import RealmSwift
class CategoryModel: Object{
dynamic var categoryID = NSUUID().uuidString
dynamic var sortOrder = 0
dynamic var name = ""
let gearList = LinkingObjects(fromType: GearModel.self, property: "category")
override class func primaryKey() -> String? {return "categoryID"}
}
类别存储库类:在线上发生致命错误"让realm = ...."在allCategories()函数
中import Foundation
import RealmSwift
class CategoryRepository: CategoryModel{
class func GetGearItemsForCategory(CategoryID: String) -> Results<GearModel>
{
let realm = try! Realm()
var gearItems = realm.objects(GearModel.self)
let predicate = NSPredicate(format: "categoryID = %@", CategoryID)
gearItems = gearItems.filter(predicate)
return gearItems
}
class func GetCountOfGearItems(CategoryID: String) -> Int
{
let realm = try! Realm()
var gearItems = realm.objects(GearModel.self)
let predicate = NSPredicate(format: "categoryID = %@", CategoryID)
gearItems = gearItems.filter(predicate)
return gearItems.count
}
class func AddCategory(CategoryID: String, Name: String, SortOrder: Int){
let realm = try! Realm()
let category = CategoryModel(name: CategoryID, categoryID: Name, sortOrder: SortOrder)
try! realm.write {
realm.add(category)
}
}
class func allCategories() -> Results<CategoryModel> {
let realm = try! Realm()
return realm.objects(CategoryModel.self).sorted(byProperty: "SortOrder")
}
答案 0 :(得分:1)
不支持从具有LinkingObjects
属性的类继承。
Realm中的Object和List属性不是协变的,这意味着它们只能指向指定的类,而不是该类的子类。这意味着GearModel.category
无法链接到CategoryRepository
个对象,但CategoryRepository
具有继承的gearList
属性,该属性尝试列出链接到它的GearModel
个对象。
在您的具体情况下,不清楚为什么CategoryRepository
继承自CategoryModel
。