objective-c - 在Objective-C中使用精确的int字典值过滤字典的NSMutableArray
问题描述
我有一个包含 nsdictionary 对象的 NSMutableArray。
`[
{
"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": "kaiserahmedkhan@gmail.com",
"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": "nasreenara65@gmail.com",
"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
}
]
我正在尝试使用键 UserId 过滤数组。但无法过滤。我尝试过这种方式。
NSString* userId=userInfo[@"UserId"];
NSPredicate* predicate=[NSPredicate predicateWithFormat:@"(UserId == %@)",userId]; NSArray* filteredArrays = [allData filteredArrayUsingPredicate:predicate];
这里 UserId 包含字符串格式的整数值。在尝试使用提供的代码片段进行过滤时,它总是返回 O 对象。
解决方案
您不能将字符串与这样的数字进行比较。所以只需取 intValue 并将其作为 int 进行比较。
NSString *userId = userInfo[@"UserId"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UserId = %d", userId.intValue];
NSArray *filtered = [allData filteredArrayUsingPredicate:predicate];
推荐阅读
- sql-server - 指数策略 - 表设计每小时数据
- python - 在 RPG 战斗系统中,当我输入“攻击”时,如果我输入“运行”,它就会运行代码
- java - 复制项目后 Eclipse 引用其他项目中的文件
- amazon-web-services - 私有子网中的redshift集群节点有公网IP
- javascript - 重定向后的空 PHP 会话数组
- javascript - 不活动时如何删除css样式
- android - 将我的 Bmi 结果添加到 firebase 数据
- spring-boot - 类 java.time.LocalDateTime 不能转换为类 java.lang.String
- javascript - 如何避免在循环中等待
- c - 是否可以使用“&”符号代替指针?