使用预构建字符串创建谓词

发布于 2024-11-06 16:52:01 字数 845 浏览 3 评论 0原文

有没有办法直接从预先格式化的字符串创建 nspredicate 而无需调用 predicateWithFormat?最终字符串如下所示:

(ineptic=1) AND (dischargedate!=) AND ((attending=SMITH) OR (admitting=SMITH) OR (consulting contains[cd] SMITH) OR (出席=JONES)或(承认=JONES)或(咨询包含[cd] JONES))

    NSMutableString *preds = [[NSMutableString alloc] initWithString:@""];

    NSArray *provs = [self.providerCode componentsSeparatedByString:@"|"];
    for (NSString *prov in provs) {
        [preds appendFormat:@" (attending=%@) OR (admitting=%@) OR (consulting contains[cd] %@) ", prov, prov, prov];
    }
    NSString *final = [NSString stringWithFormat:@"(inpatient=%@) AND (dischargedate!=%@) AND (%@)", [NSNumber numberWithBool: self.inpatients], [NSNull null], preds];

    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:final]];

Is there a way to create an nspredicate directly from a pre-formatted string without calling predicateWithFormat? The final string would look something like:

(inpatient=1) AND (dischargedate!=<null>) AND ((attending=SMITH) OR (admitting=SMITH) OR (consulting contains[cd] SMITH) OR (attending=JONES) OR (admitting=JONES) OR (consulting contains[cd] JONES))

    NSMutableString *preds = [[NSMutableString alloc] initWithString:@""];

    NSArray *provs = [self.providerCode componentsSeparatedByString:@"|"];
    for (NSString *prov in provs) {
        [preds appendFormat:@" (attending=%@) OR (admitting=%@) OR (consulting contains[cd] %@) ", prov, prov, prov];
    }
    NSString *final = [NSString stringWithFormat:@"(inpatient=%@) AND (dischargedate!=%@) AND (%@)", [NSNumber numberWithBool: self.inpatients], [NSNull null], preds];

    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:final]];

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

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

发布评论

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

评论(1

被翻牌 2024-11-13 16:52:01

是的,可以,但是您需要稍微修改格式字符串。

而不是这样做:

[preds appendFormat:@" (attending = %@)", prov];

您需要这样做:

[preds appendFormat:@" (attending = '%@')", prov];

请注意 %@ 修饰符周围使用单引号。这就是谓词如何知道它是一个常量值的方式。

然而,即使您走这条路,您仍然会陷入使用 predicateWithFormat: 的困境,而您似乎想避免这种情况。您还可能会遇到如何在格式字符串中使用 NSNull 的问题。

我建议做类似这样的事情:

NSArray *provs = [self.providerCode componentsSeparatedByString:@"|"];

NSMutableArray *providerPredicates = [NSMutableArray array];
NSPredicate *template = [NSPredicate predicateWithFormat:@"attending = $prov OR admitting = $prov OR consulting CONTAINS[cd] $prov"];
for (NSString *prov in provs) {
  NSDictionary *substitutions = [NSDictionary dictionaryWithObject:prov forKey:@"prov"];
  NSPredicate *p = [template predicateWithSubstitutionVariables:substitutions];
  [providerPredicates addObject:p];
}

NSPredicate *final = [NSPredicate predicateWithFormat:@"inpatient = 1 AND dischargedate != nil"];

if ([providerPredicates count] > 0) {
  NSPredicate *providers = nil;
  if ([providerPredicates count] > 1) {
    providers = [NSCompoundPredicate orPredicateWithSubpredicates:providerPredicates];
  } else {
    providers = [providerPredicates objectAtIndex:0];
  }

  final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:final, providers, nil]];
}

这是使用几个不同的巧妙的东西:

  1. 谓词变量。您解析格式字符串 @"attending = $prov OR grantting = $prov OR Advisory CONTAINS[cd] $prov" 一次,然后只需用新值替换 $prov 每次你有一个不同的提供者
  2. 构建复合谓词。您可以使用 NSCompoundPredicate 上的一些类方法将多个谓词转换为单个分组的 OR 或 AND 谓词。

Yes you can, but you need to modify the format string slightly.

Instead of doing:

[preds appendFormat:@" (attending = %@)", prov];

You'd need to do:

[preds appendFormat:@" (attending = '%@')", prov];

Note the use of single-quotes around the %@ modifier. That's how the predicate knows it's a constant value.

However, even if you go this route, you're still stuck using predicateWithFormat:, which you appear to want to avoid. You'll also likely have issues with how you're using NSNull in the format string.

I would recommend doing something more like this:

NSArray *provs = [self.providerCode componentsSeparatedByString:@"|"];

NSMutableArray *providerPredicates = [NSMutableArray array];
NSPredicate *template = [NSPredicate predicateWithFormat:@"attending = $prov OR admitting = $prov OR consulting CONTAINS[cd] $prov"];
for (NSString *prov in provs) {
  NSDictionary *substitutions = [NSDictionary dictionaryWithObject:prov forKey:@"prov"];
  NSPredicate *p = [template predicateWithSubstitutionVariables:substitutions];
  [providerPredicates addObject:p];
}

NSPredicate *final = [NSPredicate predicateWithFormat:@"inpatient = 1 AND dischargedate != nil"];

if ([providerPredicates count] > 0) {
  NSPredicate *providers = nil;
  if ([providerPredicates count] > 1) {
    providers = [NSCompoundPredicate orPredicateWithSubpredicates:providerPredicates];
  } else {
    providers = [providerPredicates objectAtIndex:0];
  }

  final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:final, providers, nil]];
}

This is using a couple different neat things:

  1. Predicate variables. You parse the format string @"attending = $prov OR admitting = $prov OR consulting CONTAINS[cd] $prov" once, and then simply substitute in new values for $prov each time you have a different provider
  2. Constructing compound predicates. You use some class methods on NSCompoundPredicate to turn multiple predicates into a single, grouped OR or AND predicate.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文