从字符串创建 NSPredicate

发布于 2024-11-05 13:56:09 字数 574 浏览 7 评论 0原文

我需要从多个值数组形成一个谓词。所以我想我可以首先形成一个包含所有值的字符串,然后传递该字符串以在谓词中使用,即

NSString* stringForPredicate  = [[NSString alloc] init];
if (fromDate != nil) {
          stringForPredicate = [stringForPredicate stringByAppendingFormat:@"(Date > %@ AND Date < %@)", fromDate, toDate];
    }

我需要进行进一步的计算来形成最终的谓词字符串。

现在,我希望谓词使用这个字符串。我认为这样的东西会起作用:

   NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:@"%@",stringForPredicate];

但它不会并抛出异常:

“无法解析格式字符串“%@””

有没有办法让这个工作起作用?

谢谢,

I need to form a predicate from multiple arrays of values. So I thought I can initially form a string with all the values, and then pass that string for usage in a predicate, i.e

NSString* stringForPredicate  = [[NSString alloc] init];
if (fromDate != nil) {
          stringForPredicate = [stringForPredicate stringByAppendingFormat:@"(Date > %@ AND Date < %@)", fromDate, toDate];
    }

There are further calculations that I do to form the final predicate string.

Now, I want the predicate to use this string. I thought that something like this would work:

   NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:@"%@",stringForPredicate];

But it doesnt and throws the exception:

'Unable to parse the format string "%@"'

Is there a way I can make this work?

thanks,

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

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

发布评论

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

评论(3

肩上的翅膀 2024-11-12 13:56:09

stringForPredicate 变量实际上包含format_string。因此,您需要分配该变量来代替 format_string,并在其后传递 args,用逗号分隔,如下所示。

NSSring *stringForPredicate = @"(Date > %@ AND Date < %@)";
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:stringForPredicate, fromDate, toDate];

对于复合谓词:

NSMutableArray *subPredicates = [NSMutableArray array];

if (fromDate != nil) {

    NSPredicate *from_predicate = [NSPredicate predicateWithFormat:@"Date > %@", fromDate];
    [subPredicates addObject:from_predicate];
}

if (toDate != nil) {

    NSPredicate *to_predicate = [NSPredicate predicateWithFormat:@"Date < %@", toDate];
    [subPredicates addObject:to_predicate];
}

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];

The stringForPredicate variable actually contains the format_string. So, you need to assign that variable in place of the format_string, and pass the args after that, seperated by commas, like this.

NSSring *stringForPredicate = @"(Date > %@ AND Date < %@)";
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:stringForPredicate, fromDate, toDate];

For compound predicates:

NSMutableArray *subPredicates = [NSMutableArray array];

if (fromDate != nil) {

    NSPredicate *from_predicate = [NSPredicate predicateWithFormat:@"Date > %@", fromDate];
    [subPredicates addObject:from_predicate];
}

if (toDate != nil) {

    NSPredicate *to_predicate = [NSPredicate predicateWithFormat:@"Date < %@", toDate];
    [subPredicates addObject:to_predicate];
}

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
匿名的好友 2024-11-12 13:56:09

这个不行吗?

  NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:stringForPredicate];

或者您必须

NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@",stringForPredicate]];

参考文档

格式字符串摘要

区分很重要
a 中不同类型的值
格式字符串。另请注意,单
或双引号变量(或
替换变量字符串)将
导致 %@、%K 或 $variable 成为
解释为格式中的文字
字符串等将阻止任何
替换。

试试这个

stringForPredicate = [stringForPredicate stringByAppendingFormat:@"(Date > %@) AND (Date < %@)", fromDate, toDate];

Wont this do it?

  NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:stringForPredicate];

or you have to do this

NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@",stringForPredicate]];

From the reference documents

Format String Summary

It is important to distinguish between
the different types of value in a
format string. Note also that single
or double quoting variables (or
substitution variable strings) will
cause %@, %K, or $variable to be
interpreted as a literal in the format
string and so will prevent any
substitution.

Try this

stringForPredicate = [stringForPredicate stringByAppendingFormat:@"(Date > %@) AND (Date < %@)", fromDate, toDate];
三生殊途 2024-11-12 13:56:09

对于那些使用 swift 的人:

let predicate = NSPredicate.init("Date > %@ AND Date < %@", fromDate, toDate);

For those using swift:

let predicate = NSPredicate.init("Date > %@ AND Date < %@", fromDate, toDate);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文