我正在使用PredicateEditor,但我正在努力让复合谓词起作用。
我有一个类似下面的谓词:
我希望能够嵌套顶级谓词(即“以下所有都是真的”),这样我就可以进行如下搜索:a为真且b为真AND(c为真或者d为真)。
我在代码中为谓词设置模板(因为我无法让IB工作),如下所示:
if let editor = predicateEditor{
var stringExpressions: [NSExpression] = []
var stringOperators: [NSNumber] = []
for p in LTDEddingtonNumberProperty.StringProperties{
stringExpressions.append(NSExpression(forKeyPath: p.rawValue))
}
stringOperators.append(NSNumber(value:NSComparisonPredicate.Operator.contains.rawValue))
stringOperators.append(NSNumber(value:NSComparisonPredicate.Operator.equalTo.rawValue))
stringOperators.append(NSNumber(value:NSComparisonPredicate.Operator.notEqualTo.rawValue))
let stringTemplate = NSPredicateEditorRowTemplate.init(leftExpressions: stringExpressions, rightExpressionAttributeType: NSAttributeType.stringAttributeType, modifier: NSComparisonPredicate.Modifier.direct, operators: stringOperators, options: Int(NSComparisonPredicate.Options.caseInsensitive.rawValue))
let booleanExpressions: [NSExpression] = [NSExpression(forKeyPath: LTDEddingtonNumberProperty.isWeekDay.rawValue)]
var booleanOperators: [NSNumber] = []
booleanOperators.append(NSNumber(value:NSComparisonPredicate.Operator.equalTo.rawValue))
booleanOperators.append(NSNumber(value:NSComparisonPredicate.Operator.notEqualTo.rawValue))
let booleanTemplate = NSPredicateEditorRowTemplate.init(leftExpressions: booleanExpressions, rightExpressionAttributeType: NSAttributeType.booleanAttributeType, modifier: NSComparisonPredicate.Modifier.direct, operators: booleanOperators, options: Int(NSComparisonPredicate.Options.caseInsensitive.rawValue))
let compoundTemplate = NSPredicateEditorRowTemplate.init(compoundTypes: [NSNumber(value: NSCompoundPredicate.LogicalType.and.rawValue), NSNumber(value: NSCompoundPredicate.LogicalType.or.rawValue), NSNumber(value: NSCompoundPredicate.LogicalType.not.rawValue)])
editor.rowTemplates.append(contentsOf: [stringTemplate, booleanTemplate, compoundTemplate])
}
我猜测设置复合谓词模板存在问题,但无法弄清楚是什么。
有什么建议吗?
我正在使用Swift 4和Xcode 9