在Objective-C中使用精确的int字典值过滤字典的NSMutableArray
Posted
技术标签:
【中文标题】在Objective-C中使用精确的int字典值过滤字典的NSMutableArray【英文标题】:Filter NSMutableArray of dictionary with an exact int dictionary value in Objective-C 【发布时间】:2019-12-19 09:20:36 【问题描述】:我有一个包含 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 包含字符串格式的整数值。在尝试使用提供的代码 sn-ps 进行过滤时, 它总是返回 O 对象。
【问题讨论】:
"UserId 包含字符串格式的整数值" 你确定吗?如果你改用@"(UserId == %@)", @([userId intValue])]
?
【参考方案1】:
您不能将字符串与这样的数字进行比较。所以只需取 intValue 并将其作为 int 进行比较。
NSString *userId = userInfo[@"UserId"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UserId = %d", userId.intValue];
NSArray *filtered = [allData filteredArrayUsingPredicate:predicate];
【讨论】:
以上是关于在Objective-C中使用精确的int字典值过滤字典的NSMutableArray的主要内容,如果未能解决你的问题,请参考以下文章
在 Objective-c 中使用 AFNetworking 对字典进行 URL 编码
Objective-C如何使用标题中的嵌套字典将数据发送到Http Post请求
Objective-c:自定义类实例作为字典键,当键存在时返回 nil
如何在使用桥接头时将数据(字典)从 Objective-C 控制器传递到 Swift `viewcontroller` 中?还有其他方法吗?