iOSDev
iOSDev

Reputation: 412

SUBQuery NSPredicate

I have a an Array of Dictionaries, in which phone numbers is one key,

{
        Email = "[email protected]";
        Firstname = Sourish;
        Lastname = Sam;
        Middlename = S;
        phonenumbers =         (
                        {
                Home = 3452345345;
                Main = 4985739804;
                Mobile = 3567567741;
                Other = 5769875698;
                Test = 2290834709;
                Testing = 9336664648;
                Work = 4523523453;
                iPhone = 3453245234;
            }
        );
        recordID = 66;
    }

I am searching the array with this string "stringToSearch". This is the way I am trying to get the first name while searching

NSMutableArray *allPredicatesArray = [NSMutableArray new];
NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF.Firstname beginswith[c] %@",stringToSearch];
NSArray *firstNameFilteredArray = [self.recordsMutableArray filteredArrayUsingPredicate:firstNamePredicate];
[allPredicatesArray addObjectsFromArray:firstNameFilteredArray];

Now, can i get using predicates the "Mobile" number search using NSpredicates

Upvotes: 0

Views: 599

Answers (3)

David Berry
David Berry

Reputation: 41226

If phone numbers is not actually an array of dictionaries, I think what you're wanting is the predicate string SELF.phonenumbers.Mobile beginswith[c] %@"

If it actually is an array of dictionaries, then the following will work:

@"SUBQUERY(phonenumbers, $x, $x.Mobile BEGINSWITH %@).@count > 0"

Upvotes: 1

Martin R
Martin R

Reputation: 539685

"phonenumbers" is an array, therefore you have to use "ANY" in the predicate:

NSString *numberToSearch = @"3567567741";
NSPredicate *mobileNumberPredicate = [NSPredicate predicateWithFormat:@"ANY phonenumbers.Mobile BEGINSWITH %@", numberToSearch];

assuming that the number is stored as a string.

Upvotes: 1

iPatel
iPatel

Reputation: 47049

Just try with following code:

NSPredicate *predicateProduct = [NSPredicate predicateWithFormat:@"Mobile BEGINSWITH[cd] %@", stringToSearch];
NSArray *filteredArray = [self.listOfProducts filteredArrayUsingPredicate:predicateProduct];
NSLog(@"%@", filteredArray);

Upvotes: 0

Related Questions