ios - 通过 NSDate 在 Realm 中过滤查询抛出 NSInvalidArgumentException

标签 ios swift predicate realm

我到处都看了,甚至访问了一些可疑的网站,这些网站的病毒警告消息永远不会消失,但我无法弄清楚这一点。

我只是想过滤 Results<T>按日期对象:

let messages = realm.objects(RMChatMessage).filter("timestamp > \(date)) AND (timestamp <= \(to))"))

每当运行此行时,它都会引发以下内容:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "timestamp > 1970-01-01 00:00:00 +0000"'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010fba8c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000011174ebb7 objc_exception_throw + 45
    2   Foundation                          0x000000010ffb66bd _qfqp2_performParsing + 8495
    3   Foundation                          0x000000010ffb4526 +[NSPredicate predicateWithFormat:arguments:] + 46
...

我尝试使用 NSDateFormatter格式如 yyyy-MM-dd hh:mm:ss , 或 date.description , 使用 NSPredicate(format:...)而不是 Result<T>.filter(...) , 等等,但没有任何效果。

这是 Realm 中的一些错误吗?

最佳答案

let messages = realm.objects(RMChatMessage).filter("timestamp > %@ AND timestamp <= %@", date, to)

NSPredicate对 Swift 字符串插值没有任何特殊处理,也不支持直接在格式字符串中写入日期。

关于ios - 通过 NSDate 在 Realm 中过滤查询抛出 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31952669/

相关文章:

ios - 文本字段在 iOS 模拟器上不显示键盘

iOS/swift : how to detect touch action on a UITextField

ios - 一次从所有 Collection View 单元格内的 TextView 中获取文本

predicate - 什么是归纳谓词?

c# - 封装谓词的模式/实践

javascript - 谁能帮我分离世博会应用程序

ios - 通过 Azure DevOps 构建 Ionic Cordova ios

iOS AppDelegate 添加supportedInterfaceOrientationsForWindow

Swift 3/Xcode 文本对齐到 imageview

list - 将列表 1 中的特定数字与列表 2 中的特定数字交换