gpt4 book ai didi

ios - RealmSwift 查询中带有内部对象引用的谓词问题

转载 作者:行者123 更新时间:2023-11-29 11:29:55 26 4
gpt4 key购买 nike

我有一个 Report 类,例如:

class Report: Object{

@objc dynamic var id : String? = nil
@objc dynamic var studentProfileId : String? = nil
@objc dynamic var createdAt : String? = nil
@objc dynamic var updatedAt : String? = nil
@objc dynamic var type : String? = nil
@objc dynamic var status : String? = nil
@objc dynamic var assessment : AssessmentLocal? = nil
var avgScore = RealmOptional<Double>(0)
}

Report类有一个AssessmentLocal类的评估变量。

class AssessmentLocal: Object{

@objc dynamic var id : String?
@objc dynamic var status : String?
@objc dynamic var type : String?
@objc dynamic var code : String?
@objc dynamic var cycle = 0
var year = RealmOptional<Int>(0)
@objc dynamic var startDate : String?
@objc dynamic var grade : String?
}

我正在尝试使用以下谓词进行查询:

let predicate = NSPredicate(format: "studentProfileId = %@ AND assessment.year = %@ AND assessment.cycle >= %@", report.studentProfileId!, String((report.assessment?.year.value)!), String((report.assessment?.cycle)!))

let reports = getDatabase().objects(Report.self).filter(predicate).sorted(byKeyPath: "assessment.cycle")

每当我尝试获取已保存的报告时,我都会崩溃。

"terminating with uncaught exception of type NSException".

我无法弄清楚这一点。

最佳答案

错误的发生可能是因为您的类型与 Realm 期望的类型不同。

assessment.cycleassessment.year 是整数,因此您不应使用 %@ 作为它们的占位符。 %@ 用于字符串。对于整数,您应该使用 %i

您还应该删除参数中的字符串转换。

let predicate = NSPredicate(format: "studentProfileId = %@ AND assessment.year = %i AND assessment.cycle >= %i", 
report.studentProfileId!,
report.assessment?.year.value!,
report.assessment?.cycle!
)

关于ios - RealmSwift 查询中带有内部对象引用的谓词问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54547175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com