NSDate在Realm中过滤查询会引发NSInvalidArgumentException [英] Filtering query in Realm by NSDate throws NSInvalidArgumentException

查看:104
本文介绍了NSDate在Realm中过滤查询会引发NSInvalidArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处都是,甚至到了一些带有病毒警告消息的可疑站点,这些消息永远都不会消失,我无法弄清.

I've looked everywhere, even hitting some dubious sites with virus warning messages that never goes away, and I can't figure this out.

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

I'm simply trying to filter Results<T> object by date:

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

只要运行此行,它就会引发以下内容:

And whenever this line is run, it raises the following:

*** 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:ssdate.description,使用NSPredicate(format:...)代替Result<T>.filter(...),依此类推,但没有任何效果.

I tried using NSDateFormatter with formats like yyyy-MM-dd hh:mm:ss, or date.description, using NSPredicate(format:...) instead of Result<T>.filter(...), and so on, yet nothing worked.

这是Realm中的一些错误吗?

Is this some bug in Realm?

推荐答案

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

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

NSPredicate does not have any special handling for Swift string interpolation and doesn't support writing dates directly within the format string.

这篇关于NSDate在Realm中过滤查询会引发NSInvalidArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆