gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:49:36 25 4
gpt4 key购买 nike

有人可以帮我定义一个仅返回 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];

最佳答案

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

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

乔斯。

关于core-data - NSPredicate 用于 NSManagedObject 的字符串属性的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3644621/

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