0

I am trying to predicate the nested objects value here is a structure of nested objects.

@interface APContact : NSObject
@property (nullable, nonatomic, strong) NSArray <APPhone *> *phones;
@end

@interface APPhone : NSObject
@property (nullable, nonatomic, strong) NSString *number;
@end

I am trying to predicate like this as show in below block of code. I want to predicate the number string. how to do this

for (APContact *contact in duplucateDataArray) {
    NSMutableArray *countArray = [[NSMutableArray alloc] init];
    NSPredicate *morningAttendees  = [NSPredicate predicateWithFormat:@"SELF.%K.%K MATCHES %@",@"phones[0]",@"number", contact.phones[0].number];
    NSArray <APContact*> *predicateContact = [duplucateDataArray filteredArrayUsingPredicate:morningAttendees];
    NSLog(@"predicate contact%@",predicateContact);
}

On running above code getting below exception

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<APContact 0x2824da6d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key phones[0].'
 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object (
    "+91 88815 12534"
).

Any suggestions would be more appreciated.

2 Answers 2

1

Remove phones[0] as error says you can not use phones[0] APContact has the key phones.

Use below line -

 NSPredicate *morningAttendees  = [NSPredicate predicateWithFormat:@"SELF.%K.%K MATCHES %@",@"phones",@"number", contact.phones[0].number];
Sign up to request clarification or add additional context in comments.

1 Comment

This give me error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object ( "+1 (888) 123-1725" ).'
0

Replace MATCHES with LIKE.

MATCHES assumes your argument is a regex, LIKE assumes your argument is string with wildcard symbols. source

Also, when using nested predicates use ANY, SOME or ALL operators.

NSPredicate *morningAttendees  = [NSPredicate predicateWithFormat:@"ANY phones.number LIKE %@",contact.phones[0].number];

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.