gpt4 book ai didi

cocoa - NSPredicateEditorRowTemplate 和 CoreData

转载 作者:行者123 更新时间:2023-12-03 16:56:04 24 4
gpt4 key购买 nike

我正在尝试为我的核心数据实体生成谓词编辑器模板。在我的代码中,我有以下内容:

 NSEntityDescription *descrip = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];
NSArray *templates = [NSPredicateEditorRowTemplate templatesWithAttributeKeyPaths:[NSArray arrayWithObjects:@"name", @"age", nil] inEntityDescription:descrip];

[ibPredicateEditor setRowTemplates: templates];

NSPredicate *p = [NSPredicate predicateWithFormat:@"name like 'John'"];

[ibPredicateEditor setObjectValue:p];

打印出模板数组的内容给出以下内容:

CFArray 0x1002d7400 [0x7fff70ff5f20] {type = immutable, count = 2, values = ( 0 : NSPredicateEditorRowTemplate 0x10025c090: [name] [99, 4, 5, 8, 9] NSStringAttributeType 1 : NSPredicateEditorRowTemplate 0x1002d2dc0: [age] [4, 5, 0, 2, 1, 3] NSInteger16AttributeType )}

执行此代码时,我会在控制台上看到以下内容:

Warning - unable to find template matching predicate name LIKE "John"

执行此操作的界面看起来非常简单,所以我似乎无法弄清楚我做错了什么。任何帮助将不胜感激!

编辑

我最初的问题是,当我的模板不支持 LIKE 运算符时,我正在使用它。然而,我很困惑为什么在将复合谓词传递到编辑器时会收到类似的警告。

NSPredicate *p = [NSPredicate predicateWithFormat:@"name CONTAINS 'John'"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"name CONTAINS 'Jo'"];
NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:p, p2, nil]];
[ibPredicateEditor setObjectValue: final];

NSPredicate *final = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:p, p2, nil]];
[ibPredicateEditor setObjectValue: final];

这两个都会产生与我最初的问题类似的警告。但是,我发现很奇怪的是,我可以使用单个谓词并构建复合和谓词,但无法将预构建的复合和谓词传递到编辑器中。

最佳答案

NSPredicateEditorRowTemplateNSString 键路径提供的默认模板不支持与 LIKE 运算符进行比较。

在您生成的模板中,有一个它支持的运算符列表:

NSPredicateEditorRowTemplate 0x10025c090: [name] [99, 4, 5, 8, 9] NSStringAttributeType

[99,4,5,8,9]表示nameNSString属性支持:

4 – 等于
5 – 不等于
8 – 开头为
9 – 结尾为
99 – 包含

(在 NSComparisonPredicate.h 中找到)

CONTAINS 与 LIKE 类似,但不带 % 和 _ 替换。如果您需要,您始终可以初始化自己的模板数组。

语法有点冗长。

NSExpression *nameExpression = [NSExpression expressionForKeyPath:@"name"];
NSArray *operators = [NSArray arrayWithObjects:
[NSNumber numberWithInt: NSEqualToPredicateOperatorType],
[NSNumber numberWithInt:NSNotEqualToPredicateOperatorType],
[NSNumber numberWithInt:NSLikePredicateOperatorType],
[NSNumber numberWithInt:NSBeginsWithPredicateOperatorType],
[NSNumber numberWithInt:NSEndsWithPredicateOperatorType],
[NSNumber numberWithInt:NSContainsPredicateOperatorType],
nil];

NSUInteger options = (NSCaseInsensitivePredicateOption |
NSDiacriticInsensitivePredicateOption);

NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc]
initWithLeftExpressions:[NSArray arrayWithObject:nameExpression]
rightExpressionAttributeType:NSStringAttributeType
modifier:NSDirectPredicateModifier
operators:operators
options:options];

关于cocoa - NSPredicateEditorRowTemplate 和 CoreData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2139939/

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