Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>NSPredicate</code> <em>does override</em> <code>isEqual</code>, but this seems to be useful only in very simple cases:</p> <pre><code>NSPredicate *pa = [NSPredicate predicateWithFormat:@"foo = 'bar'"]; NSPredicate *pb = [NSPredicate predicateWithFormat:@"foo = 'bar'"]; BOOL b = [pa isEqual:pb]; // --&gt; YES </code></pre> <p>In this case you could put an object into a dictionary using key <code>pa</code> and get the object back using key <code>pb</code>.</p> <p>But this does not work with block-based predicates at all, even if they reference the <em>same block</em>:</p> <pre><code>BOOL (^block)(id evaluatedObject, NSDictionary *bindings) = ^BOOL(id evaluatedObject, NSDictionary *bindings) { return [evaluatedObject isKindOfClass:[NSString class]]; }; NSPredicate *pa = [NSPredicate predicateWithBlock:block]; NSPredicate *pb = [NSPredicate predicateWithBlock:block]; BOOL b = [pa isEqual:pb]; // --&gt; NO </code></pre> <p>And even if that would work, it would require in your case that the two <em>blocks</em> in the predicate are recognized as equal, which is not the case:</p> <pre><code>BOOL (^block1)(id evaluatedObject, NSDictionary *bindings) = ^BOOL(id evaluatedObject, NSDictionary *bindings) { return [evaluatedObject isKindOfClass:[NSString class]]; }; BOOL (^block2)(id evaluatedObject, NSDictionary *bindings) = ^BOOL(id evaluatedObject, NSDictionary *bindings) { return [evaluatedObject isKindOfClass:NSClassFromString(@"NSString")]; }; BOOL b = [block1 isEqual:block2]; // --&gt; NO </code></pre> <p>Finally it turns out that two <em>blocks with the same body</em> are not equal:</p> <pre><code>BOOL (^block1)(id evaluatedObject, NSDictionary *bindings) = ^BOOL(id evaluatedObject, NSDictionary *bindings) { return [evaluatedObject isKindOfClass:[NSString class]]; }; BOOL (^block2)(id evaluatedObject, NSDictionary *bindings) = ^BOOL(id evaluatedObject, NSDictionary *bindings) { return [evaluatedObject isKindOfClass:[NSString class]]; }; BOOL b = [block1 isEqual:block2]; // --&gt; NO </code></pre> <p>So you can use a predicates as a key in a dictionary, but at least for block-based predicates this is pretty useless.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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