问题
I've looked everywhere, even hitting some dubious sites with virus warning messages that never goes away, and I can't figure this out.
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
...
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.
Is this some bug in Realm?
回答1:
let messages = realm.objects(RMChatMessage).filter("timestamp > %@ AND timestamp <= %@", date, to)
NSPredicate does not have any special handling for Swift string interpolation and doesn't support writing dates directly within the format string.
来源:https://stackoverflow.com/questions/31952669/filtering-query-in-realm-by-nsdate-throws-nsinvalidargumentexception