Request filtering in Realm using NSDate raises an NSInvalidArgumentException

I searched everywhere, even on some dubious sites with warning messages about viruses that never disappear, and I can't figure it out.

I am just trying to filter the Results<T> object by date:

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

And whenever this line is launched, it calls 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(...) etc., but nothing succeeded.

Is this some bug in Realm?

+4
source share
1 answer

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

NSPredicate has no special handling for interpolating Swift strings and does not support recording dates directly in the format string.

+8
source

All Articles