SUBQuery NSPredicate

时间:2014-04-21 14:04:53

标签: ios objective-c cocoa-touch nspredicate

我有一个字典数组,其中电话号码是一键,

{
        Email = "ryuiioo@uioo.njk";
        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;
    }

我正在使用此字符串“ stringToSearch ”搜索数组。 这是我在搜索时尝试获取名字的方式

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

现在,我可以使用NSpredicates

使用谓词“移动”号码搜索

3 个答案:

答案 0 :(得分:1)

“phonenumbers”是数组,因此您必须在谓词中使用“ANY”:

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

假设数字存储为字符串。

答案 1 :(得分:1)

如果电话号码实际上不是一组字典,我认为你想要的是谓词字符串SELF.phonenumbers.Mobile beginswith[c] %@"

如果它实际上是一个字典数组,那么以下内容将起作用:

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

答案 2 :(得分:0)

尝试使用以下代码:

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