NSPredicate 用于 NSManagedObject 的字符串属性的长度

发布于 2024-09-17 04:20:25 字数 1014 浏览 7 评论 0原文

有人可以帮我定义一个仅返回 NSManagedObject 的“字母”属性长度在一定范围内的谓词吗?

这是我一直在尝试的示例,我有一种感觉,这是字母。长度符号,我也尝试了 kvc 字母。@长度,但没有成功。我做错了什么?

NSManagedObjectContext *context = ...;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Word" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"letters" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"letters.length BETWEEN %@", [NSArray arrayWithObjects: [NSNumber numberWithInt:5], [NSNumber numberWithInt:20], nil]];
[fetchRequest setPredicate:predicate];

NSUInteger limit = 20;
[fetchRequest setFetchLimit:limit];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

Could someone please help me define a predicate that returns only NSManagedObject's who's "letters" attribute length is within a certain range?

Here's the example I've been trying, I've got a feeling it's the letters.length notation, I've also tried the kvc letters.@length with no success.. What am I doing wrong?

NSManagedObjectContext *context = ...;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Word" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"letters" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"letters.length BETWEEN %@", [NSArray arrayWithObjects: [NSNumber numberWithInt:5], [NSNumber numberWithInt:20], nil]];
[fetchRequest setPredicate:predicate];

NSUInteger limit = 20;
[fetchRequest setFetchLimit:limit];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦亦如幻 2024-09-24 04:46:48

我有一些坏消息。你不能这样做。 NSPredicate 中的字符串没有长度属性(我已经找到)。

我建议您为托管对象中存储的字符串的长度添加一个属性,该属性在设置字母属性时设置。然后,您可以对该属性进行查询并返回所需的值。

I've got some bad news. You can't do this. There is no length attribute for strings in the NSPredicate (that I've been able to find).

What I would recommend you do is add an attribute for the length of the stored string in your managed object that gets set when you set the letters attribute. You can then do your query on that attribute and return the values you want.

单身情人 2024-09-24 04:39:26

不确定这个代码片段的性能如何,但这是我对你的问题的回答:

NSString *attributeName = @"letters";
NSString *attributeValue = [NSString stringWithFormat:@"'.{%d,%d}'", 5, 20];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K MATCHES %@", attributeName, attributeValue];
[fetchRequest setPredicate:predicate];

Joss。

Not sure how this code snippet is performance wise but here is my answer to your question:

NSString *attributeName = @"letters";
NSString *attributeValue = [NSString stringWithFormat:@"'.{%d,%d}'", 5, 20];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K MATCHES %@", attributeName, attributeValue];
[fetchRequest setPredicate:predicate];

Joss.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文