Mru*_*rug 5 cocoa objective-c nspredicate ios
我试图使用NSPredicate从下面显示的结构中过滤所有CustomObjects,并为其属性"isSelected"赋值true.我有一个嵌套结构,如:isSelectedProperty-Object-NSArray-NSDictionary-NSArray.
[
{
"title": "ABC",
"list": [
<CustomObject>.isSelected = true,
<CustomObject>.isSelected = true,
<CustomObject>.isSelected = true
]
},
{
"title": "ABC",
"list": [
<CustomObject>.isSelected = false,
<CustomObject>.isSelected = true,
<CustomObject>.isSelected = true
]
},
{
"title": "ABC",
"list": [
<CustomObject>.isSelected = false,
<CustomObject>.isSelected = true,
<CustomObject>.isSelected = true
]
}
]
Run Code Online (Sandbox Code Playgroud)
从这样的嵌套结构,我需要过滤所有CustomObject有isSelected = true.所以我的问题是,
请提供一些理解,以便我们了解如何实际处理此类结构.
编辑 - 非常接近解决方案
在谷歌搜索和帮助穆罕默德瓦卡斯的答案后,我成功地使用以下过滤数组
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"list.isSelected CONTAINS[c] %@",@true];
NSArray *aArray = [mutArrContacts filteredArrayUsingPredicate:aPredicate];
NSArray *UnWrapped = [aArray valueForKey:@"list"];
<__NSArrayI 0x7fc969cde360>(
<__NSArrayM 0x7fc969f54a10>(
<ContactData: 0x7fc969f7a590>,
<ContactData: 0x7fc969f8dee0>
)
,
<__NSArrayM 0x7fc969f736f0>(
<ContactData: 0x7fc969f68310>
)
,
<__NSArrayM 0x7fc969f737a0>(
<ContactData: 0x7fc969f70340>
)
,
<__NSArrayM 0x7fc969f87430>(
<ContactData: 0x7fc969f65170>
)
,
<__NSArrayM 0x7fc969f874d0>(
<ContactData: 0x7fc969f51690>
)
)
Run Code Online (Sandbox Code Playgroud)
但是现在我正在努力将这些对象过滤成单个数组
(
<ContactData: 0x7fc969f7a590>,
<ContactData: 0x7fc969f8dee0>,
<ContactData: 0x7fc969f68310>,
<ContactData: 0x7fc969f70340>,
<ContactData: 0x7fc969f65170>,
<ContactData: 0x7fc969f51690>
)
Run Code Online (Sandbox Code Playgroud)
是的,你可以像这样使用NSPredicate过滤自定义对象
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY list.isSelected = %@",@true];
NSArray *filteredArry=[[json filteredArrayUsingPredicate:predicate] copy];
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.
| 归档时间: |
|
| 查看次数: |
2691 次 |
| 最近记录: |