1

I see in Apple's documentation there's an array[FIRST] operator, which is exactly what I need to write the predicate I want. Unfortunately, the documentation doesn't provide any examples and I don't see how I can clearly put that in a predicate.

So, for instance, I'd have something like [NSPredicate predicateWithFormat:@"array[FIRST] IN %@", @[@1, @2, @3]. This, of course, throws an exception:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x100001058> valueForUndefinedKey:]: this class is not key value coding-compliant for the key array.'

Because this predicate will eventually go in a core data model file, I need to represent this in a stringly format, so any solutions involving code won't work.

6
  • The error indicates that the predicate is being evaluated against a string, not a CoreData managed object. Can you give more detail of the model and what you are trying to achieve? Commented Mar 1, 2017 at 9:21
  • @pbasdf I understand the error. That's not my question. The question is how to correctly extract the first element out of the array given a predicate. Core Data is incidental to this problem. Commented Mar 1, 2017 at 16:28
  • Which array are you trying to extract elements from? An array of managed objects? Commented Mar 1, 2017 at 16:33
  • That's right. It's going to be a fetched property on an entity in an xcdatamodel file. Commented Mar 2, 2017 at 17:50
  • OK. So you want your fetched property to be "the first object that matches this predicate"? If so, I don't think it can be done: a) fetched properties always return an array (even if only one object matches), and b) the model editor will not let you specify a sort order for the FP, so you are taking pot luck as to which matching object is the first. Commented Mar 2, 2017 at 18:29

1 Answer 1

1

The predicate needs to make sense after substituting the %@ values in the format string and substituting SELF with the object being evaluated. Your predicate says: "is the first object in an unknown symbol called 'array' contained within the literal array 1,2,3?"

Consider the following examples...

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF[FIRST] == %@", @1];
BOOL result = [predicate evaluateWithObject:@[@1, @2, @3]];
// result will be YES

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF[FIRST] == %@", @2];
BOOL result = [predicate evaluateWithObject:@[@1, @2, @3]];
// result will be NO

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@[FIRST] == SELF", @[@1, @2, @3]];
BOOL result = [predicate evaluateWithObject:@1];
// result will be YES

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@[FIRST] == SELF", @[@1, @2, @3]];
BOOL result = [predicate evaluateWithObject:@2];
// result will be NO
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.