ios - Realm Swift:对象列表不起作用
问题描述
我尝试在 Swift 中将 Realm 连接到我的食谱应用程序。我对成分有疑问:当我运行我的应用程序时,我遇到了这个错误
There must be a primary key property named '_id' on a synchronized Realm but none was found for type 'recipe_ingredients'
我认为这是因为我将成分对象键入为领域对象,并且它需要一个主键。我试图添加一个,但它说
Primary Key for class 'recipe_ingredients' has been added.
这是我的模型文件:
import RealmSwift
import Foundation
class recipe: Object {
@Persisted(primaryKey: true) var _id: ObjectId?
@Persisted var name: String?
@Persisted var category: String?
@Persisted var image: String?
@Persisted var note: Double?
@Persisted var likes: Int?
@Persisted var ingredients: List<recipe_ingredients>
@Persisted var steps: List<recipe_steps>
@Persisted var time: String?
@Persisted var units: Int?
convenience init(name: String) {
self.init()
self.name = name
}
}
class recipe_ingredients: Object{
@Persisted(primaryKey: true) var _id: ObjectId?
@Persisted var name: String?
@Persisted var quantity: String?
}
class recipe_steps: Object{
@Persisted(primaryKey: true) var _id: ObjectId?
@Persisted var step_description: String?
@Persisted var step: Int?
}
这是领域模式
{
"title": "recipe",
"properties": {
"__v": {
"bsonType": "int"
},
"_id": {
"bsonType": "objectId"
},
"category": {
"bsonType": "string"
},
"image": {
"bsonType": "string"
},
"ingredients": {
"bsonType": "array",
"items": {
"bsonType": "object",
"properties": {
"name": {
"bsonType": "string"
},
"quantity": {
"bsonType": "string"
}
}
}
},
"likes": {
"bsonType": "int"
},
"name": {
"bsonType": "string"
},
"note": {
"bsonType": "double"
},
"steps": {
"bsonType": "array",
"items": {
"bsonType": "object",
"properties": {
"description": {
"bsonType": "string"
},
"step": {
"bsonType": "int"
}
}
}
},
"time": {
"bsonType": "string"
},
"units": {
"bsonType": "int"
}
}
}
谢谢你的帮助 !
解决方案
推荐阅读
- c++ - 我可以用文件初始化 IThumbnailProvider 对象吗?
- sass - sublime text 3 sass-linter 格式问题
- javascript - 如何在 reactjs 中发出 HTTP 请求之前等待活动的 Internet 连接?
- html - 你是如何做到的,当一个 Input 的值低于 4 时,它会使文本上升并使其超过 4?
- python - 在 python 图中添加图例
- angular -
不是 Angular 模块 - wpf - 如何将 TextChangedEvent 限制为用户输入?
- android - 当我尝试创建文件时权限被拒绝 - Android8+ - 系统模式下的应用程序
- julia - 在 Julia 中重复一个整数
- python - 在 Python 中检查 CSV 的第一个单元格是否为空白