简体   繁体   中英

NSPredicate is not filtering the NSMutableArray with two dictionary in one object

I have one problem with NSPredicate . I have one NSMutableArray which contains the two dictonary in one object. The structure of array is given below.

JobInfo =     (
                {
            Department = Sales;
            EndDate = "2014-08-08 10:05:27 +0000";
            Personnel = Manager;
            StartDate = "2014-08-08 10:05:03 +0000";
        },
                {
            Department = Sales;
            EndDate = "2014-08-08 10:05:27 +0000";
            Personnel = Manager;
            StartDate = "2014-08-08 10:05:03 +0000";
            Status = Active;
        }
    );
    PersonalInfo =     {
        AddressLine1 = "Address Line 1";
        AddressLine2 = "Address Line 2";
        City = dsgfdsgfdsgf;
        Email = "fddsfh@yahoo.com";
        FirstName = JACK;
        LastName = FRIDEN;
        Status = Active;
        Zip = 222222;
    };
},
{
    JobInfo =     (
                {
            Department = ADMIN;
            EndDate = "2014-08-08 10:05:27 +0000";
            Personnel = Manager;
            StartDate = "2014-08-08 10:05:03 +0000";
        },
                {
            Department = ADMIN;
            EndDate = "2014-08-08 10:05:27 +0000";
            Personnel = Manager;
            StartDate = "2014-08-08 10:05:03 +0000";
            Status = Active;
        }
    );
    PersonalInfo =     {
        AddressLine1 = fkdfkskdl;
        AddressLine2 = safksafaskfss;
        City = dsgfdsgfdsgf;
        FirstName = JACK;
        LastName = FRIDEN;
        Email = "fddsfh@yahoo.com";
        PhoneNumber = 4456565845;
        Status = Active;
        Zip = dsgsdgsd;
    };
}

I want to filter it based on the FirstName and LastName of the PersonalInfo dictonary for that i have written the code

NSPredicate *pred = [NSPredicate predicateWithFormat:@"PersonalInfo.LastName contains[cd] %@",searchText];
self.filterArray = [NSMutableArray arrayWithArray:[self.arrayList filteredArrayUsingPredicate:pred]];

After doing this it is not returning the correct value. I don't know what i am doing wrong here. Please help me.

If you want to filter it, try this one.

NSString *searchTerm = @"FRIDEN";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"PersonalInfo.LastName == %@", searchTerm];
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filtered);

I have tried this and it is working for me..

NSArray *arrData = @[
                     @{
                         @"PersonalInfo":@{
                                 @"FirstName":@"JACK",
                                 @"LastName":@"FRIDEN",
                                 @"Email":@"f3ddsfh@yahoo.com"
                                 },
                         @"JobInfo":@{
                                 @"Department":@"ADMIN"
                                 }
                         },
                     @{
                         @"PersonalInfo":@{
                                 @"FirstName":@"JACK",
                                 @"LastName":@"Hopper",
                                 @"Email":@"f1ddsfh@yahoo.com"
                                 },
                         @"JobInfo":@{
                                 @"Department":@"ADMIN"
                                 }
                         }
                     ];


NSPredicate *pred = [NSPredicate predicateWithFormat:@"PersonalInfo.LastName contains[cd] %@",@"FRI"];
NSMutableArray *filterArray = [NSMutableArray arrayWithArray:[arrData filteredArrayUsingPredicate:pred]];

It will always check for LastName in PersonalInfo... If LastName will contains text it will be filtered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM