Search code examples
objective-cfilteringnsmutablearraynsdictionarynspredicate

Filter NSMutableArray of dictionary with an exact int dictionary value in Objective-C


I have a NSMutableArray containing nsdictionary objects.

`[
    {
        "IId": 0,
        "BookSLNo": 0,
        "RollNoOrEmpId": "0",
        "BatchId": 38,
        "Batch": "NA",
        "UserId": 20,
        "Name": "Kaiser Ahmed Khan",
        "NickName": "",
        "GenderId": 1,
        "Gender": "Male",
        "DateOfBirth": "01/Jan/2000",
        "DateOfBirthYMD": "20000001",
        "DesignationId": 8,
        "Designation": "Extra Assistant Commissioner of Taxes",
        "ZoneId": 12,
        "Zone": "Taxes Appeal Zone-1 Dhaka",
        "RangeId": 15,
        "Range": "NA",
        "Circle": "0",
        "WorkingDistrictId": 13,
        "WorkingDistrict": "Dhaka",
        "HighestDegree": "",
        "SubjectName": "",
        "University": "",
        "BloodGroupId": "1",
        "BloodGroup": "NA",
        "FatherName": "",
        "MotherName": "",
        "MarriageDate": "/Date(946713600000)/",
        "SpouseName": "",
        "SpouseBloodGroupId": "0",
        "SpouseBloodGroup": "",
        "SpouseProfession": "",
        "Child1Name": "",
        "Child1GenderId": 0,
        "Child1Gender": "",
        "Child2Name": "",
        "Child2GenderId": 0,
        "Child2Gender": "",
        "Child3Name": "",
        "Child3GenderId": 0,
        "Child3Gender": "",
        "ParmHouseNo": "",
        "ParmStreetNo": "",
        "ParmPostOffice": "",
        "ParmThana": "",
        "ParmDistrictId": 12,
        "ParmDistrict": "Cox's Bazar",
        "PresHouseNo": "",
        "PresStreetNo": "",
        "PresPostOffice": "",
        "PresThana": "",
        "PresDistrictId": 0,
        "PresDistrict": "",
        "ResidentialPhone": "",
        "Mobile": "01711372588",
        "EMailId": "[email protected]",
        "FacebookId": "",
        "SkypeId": "",
        "JoiningDate": "01/Jan/2007",
        "FirstJoinedAsId": 0,
        "FirstJoinedAs": "",
        "Picture": null,
        "PictureFamily": null,
        "UserGroupId": 2,
        "UserGroup": null,
        "SectionId": 1,
        "Section": null,
        "DepartmentId": 1,
        "Department": null,
        "CreateDate": "/Date(1573221227000)/",
        "CreatedBy": 0,
        "ModifyDate": "/Date(-62135568000000)/",
        "ModifyBy": 0,
        "ExecuteType": null,
        "PresAddress": ",,,,,,,",
        "PictureUrl": "http://taxdirectorybd.com/upload/UserImages/Profile/20.jpg",
        "PictureFamilyUrl": "http://taxdirectorybd.com/upload/UserImages/Wal/20.jpg",
        "JoiningDateYMD": "01/Jan/2007",
        "MarriageDateSTR": "01/01/2000",
        "IsFreedomFighter": null,
        "Others": null
    },
    {
        "IId": 0,
        "BookSLNo": 0,
        "RollNoOrEmpId": "0",
        "BatchId": 38,
        "Batch": "NA",
        "UserId": 21,
        "Name": "Nasreen Ara",
        "NickName": "",
        "GenderId": 2,
        "Gender": "Female",
        "DateOfBirth": "01/Jan/2000",
        "DateOfBirthYMD": "20000001",
        "DesignationId": 8,
        "Designation": "Extra Assistant Commissioner of Taxes",
        "ZoneId": 12,
        "Zone": "Taxes Appeal Zone-1 Dhaka",
        "RangeId": 15,
        "Range": "NA",
        "Circle": "0",
        "WorkingDistrictId": 13,
        "WorkingDistrict": "Dhaka",
        "HighestDegree": "",
        "SubjectName": "",
        "University": "",
        "BloodGroupId": "1",
        "BloodGroup": "NA",
        "FatherName": "",
        "MotherName": "",
        "MarriageDate": "/Date(946713600000)/",
        "SpouseName": "",
        "SpouseBloodGroupId": "0",
        "SpouseBloodGroup": "",
        "SpouseProfession": "",
        "Child1Name": "",
        "Child1GenderId": 0,
        "Child1Gender": "",
        "Child2Name": "",
        "Child2GenderId": 0,
        "Child2Gender": "",
        "Child3Name": "",
        "Child3GenderId": 0,
        "Child3Gender": "",
        "ParmHouseNo": "",
        "ParmStreetNo": "",
        "ParmPostOffice": "",
        "ParmThana": "",
        "ParmDistrictId": 0,
        "ParmDistrict": "",
        "PresHouseNo": "",
        "PresStreetNo": "",
        "PresPostOffice": "",
        "PresThana": "",
        "PresDistrictId": 0,
        "PresDistrict": "",
        "ResidentialPhone": "",
        "Mobile": "01552371912",
        "EMailId": "[email protected]",
        "FacebookId": "",
        "SkypeId": "",
        "JoiningDate": "01/Jan/2000",
        "FirstJoinedAsId": 0,
        "FirstJoinedAs": "",
        "Picture": null,
        "PictureFamily": null,
        "UserGroupId": 2,
        "UserGroup": null,
        "SectionId": 1,
        "Section": null,
        "DepartmentId": 1,
        "Department": null,
        "CreateDate": "/Date(1573221354000)/",
        "CreatedBy": 0,
        "ModifyDate": "/Date(-62135568000000)/",
        "ModifyBy": 0,
        "ExecuteType": null,
        "PresAddress": ",,,,,,,",
        "PictureUrl": "http://taxdirectorybd.com/upload/UserImages/Profile/21.jpg",
        "PictureFamilyUrl": "http://taxdirectorybd.com/upload/UserImages/Wal/21.jpg",
        "JoiningDateYMD": "01/Jan/2000",
        "MarriageDateSTR": "01/01/2000",
        "IsFreedomFighter": null,
        "Others": null
    }
    ]

I am trying to filter the array using the key UserId. But unable to filter. I tried in this way.

NSString* userId=userInfo[@"UserId"];
NSPredicate* predicate=[NSPredicate predicateWithFormat:@"(UserId == %@)",userId];  NSArray* filteredArrays = [allData filteredArrayUsingPredicate:predicate];

Here UserId contains integer value in string format. Whie trying to filter using the provided code snippets , It is always returning O objects.


Solution

  • You can't compare a string to a number like that. So just take the intValue and compare it as an int.

    NSString *userId = userInfo[@"UserId"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UserId = %d", userId.intValue];
    NSArray *filtered = [allData filteredArrayUsingPredicate:predicate];