我正在编写一个使用Realm在iOS设备上本地存储数据的应用程序。
现在,我想使用该对象的属性(documentNumber,日期等)填充列表。
我面临的问题是,当应用程序运行时,对整数进行了例外处理,对象的其他所有属性仅通过表中的名称而不是其值显示。
例如,如果我有一个对象,其name
属性的值设置为Bob
,则显示的是表格name
而不是Bob
我已经检查了数据库,它包含具有正确值的所有字段
我正在使用的代码:
import SwiftUI
struct ItemDocumentsView2: View {
var documents = queryDocuments()
var body: some View {
List{
ForEach(documents, id: \.self) { document in
Text(document.document) // String -> shows "documentNumber" instead of the actual number
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
Text(document.date) // Integer -> shows the integer correctly
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
}
}
}
}
Document类是这样的:
import Foundation
import RealmSwift
class Documents : Object, Codable{
@objc var id : CLong
@objc var document : String
@objc var date : CLong
init(id: CLong, document : String, date : CLong?){
self.id = id
self.document = document
self.date = date
}
override init(){
self.id = 10000000
self.document = "document"
self.date = 100000000
}
}
查询方法:
func queryDbForDocuments() -> [Documents]{
let realm = try! Realm()
let documents = realm.objects(Documents.self)
return Array(documents)
}
您的对象定义有缺陷,您需要向所有持久属性添加dynamic关键字。
class Documents : Object, Codable{
@objc dynamic var id: CLong
@objc dynamic var document: String
@objc dynamic var date: CLong
init(id: CLong, document: String, date: CLong){
self.id = id
self.document = document
self.date = date
}
override init(){
self.id = 10000000
self.document = "document"
self.date = 100000000
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句