gpt4 book ai didi

ios - Realm 查询 'where before date'

转载 作者:行者123 更新时间:2023-11-28 18:56:41 25 4
gpt4 key购买 nike

我刚刚发现了 Realm 并开始在 objective-c 中使用它。

我正在尝试获取对象where create_date before 特定日期和时间

我读了this answer on StackOverflow ,所以我尝试了这个:

NSString *dateString = @"2015-06-03 08:24:00";
NSDateFormatter * dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *newDate = [dateFormatter dateFromString:dateString];

RLMRealm *realm = [RLMRealm defaultRealm];
NSString *_where = [NSString stringWithFormat:@"create_date < '%@'", newDate];
RLMResults *results = [database_articles objectsInRealm:realm where:_where];

但我收到以下错误:

'Expected object of type date for property 'created' on object of type 'database_articles', but received: 2015-06-03 05:24:00 +0000'

那么如何在 Realmquery 中传递 NSDATE ??

提前致谢..

最佳答案

您使用了方法 + (RLMResults *)objectsInRealm:(RLMRealm *) where:(NSString *), ...;,这是一个方便的 API,接受一个字符串和更多的参数和即时为您构建一个 NSPredicate。通过首先向 [NSString stringWithFormat:] 提供参数,您序列化了日期,因此出现了这个错误。

使用这两种变体之一来避免这种情况:

RLMResults *results = [database_articles objectsInRealm:realm where:@"create_date < %@", newDate];

NSPredicate *_where = [NSPredicate predicateWithFormat:@"create_date < %@", newDate];
RLMResults *results = [database_articles objectsInRealm:realm withPredicate:_where];

关于ios - Realm 查询 'where before date',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31718387/

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