Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A predicate like</p> <pre><code>[NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [MyClass class]] </code></pre> <p>actually works. It returns <em>exactly the same predicate</em> as the (documented)</p> <pre><code>[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForEvaluatedObject] rightExpression:[NSExpression expressionForConstantValue:[MyClass class]] customSelector:@selector(isMemberOfClass:)] </code></pre> <p>The problem in your case could be that <code>isMemberOfClass:</code> compares for class <em>equality</em>, so this would not match objects which are members of a subclass of <code>MyClass</code>.</p> <p>This could happen if <code>MyClass</code> is a class cluster and <code>myObject</code> is an instance of a concrete subclass. For example:</p> <pre><code>NSString *myString = [[NSString alloc] initWithUTF8String:"hello world"]; Class c1 = [NSString class]; // NSString Class c2 = [myString class]; // __NSCFString BOOL b1 = [myString isMemberOfClass:c1]; // NO BOOL b2 = [myString isMemberOfClass:c2]; // YES </code></pre> <p>In that case <code>[myObject class]</code> and <code>[MyClass class]</code> are different, which would explain why one predicate works and the other predicate does not work.</p> <p>The following code uses <code>isKindOfClass:</code> instead and produces the expected result:</p> <pre><code>NSArray *array = @[@"123", @456, [NSArray array]]; NSPredicate* predicate = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [NSNumber class]]; NSArray* predicateResults = [array filteredArrayUsingPredicate:predicate]; NSLog(@"%@", predicateResults); // Output: ( 456 ) </code></pre>
    singulars
    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.
    3. 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