Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes you can, but you need to modify the format string slightly.</p> <p>Instead of doing:</p> <pre><code>[preds appendFormat:@" (attending = %@)", prov]; </code></pre> <p>You'd need to do:</p> <pre><code>[preds appendFormat:@" (attending = '%@')", prov]; </code></pre> <p>Note the use of single-quotes around the <code>%@</code> modifier. That's how the predicate knows it's a constant value.</p> <p>However, even if you go this route, you're still stuck using <code>predicateWithFormat:</code>, which you appear to want to avoid. You'll also likely have issues with how you're using <code>NSNull</code> in the format string.</p> <p>I would recommend doing something more like this:</p> <pre><code>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] &gt; 0) { NSPredicate *providers = nil; if ([providerPredicates count] &gt; 1) { providers = [NSCompoundPredicate orPredicateWithSubpredicates:providerPredicates]; } else { providers = [providerPredicates objectAtIndex:0]; } final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:final, providers, nil]]; } </code></pre> <p>This is using a couple different neat things:</p> <ol> <li>Predicate variables. You parse the format string <code>@"attending = $prov OR admitting = $prov OR consulting CONTAINS[cd] $prov"</code> once, and then simply substitute in new values for <code>$prov</code> each time you have a different provider</li> <li>Constructing compound predicates. You use some class methods on <code>NSCompoundPredicate</code> to turn multiple predicates into a single, grouped <code>OR</code> or <code>AND</code> predicate.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload