【发布时间】:2015-02-14 13:59:49
【问题描述】:
您好,我正在使用新语言 Apple Swift 并使用 NSArray:
[
{
"Section" : "Title1",
"Items" :
[
{"Nr" : "101"},
{"Nr" : "201"},
{"Nr" : "301"},
{"Nr" : "401"}
]
},
{
"Section" : "Title2",
"Items" :
[
{"Nr" : "102"},
{"Nr" : "202"},
{"Nr" : "302"}
]
},
{
"Section" : "Title3",
"Items" :
[
{"Nr" : "1102"},
{"Nr" : "2102"},
{"Nr" : "3102"}
]
}
]
我想使用搜索,只显示找到的内容
据我了解,我必须在 swift 中使用“filteredArrayUsingPredicate”和 NSPredicate,所以我的示例是:
var arr:NSArray = [...] // My array sample goes here
var pre:NSPredicate = NSPredicate(format: "ANY Items.Nr BEGINSWITH[c] %@", argumentArray: ["20"]) // We a going to search "20" in /Items/Nr
var result:NSArray = arr.filteredArrayUsingPredicate(pre)
这是正确的,但结果不是我需要的:
result =
[
{
"Section" : "Title1",
"Items" :
[
{"Nr" : "101"},
{"Nr" : "201"},
{"Nr" : "301"},
{"Nr" : "401"}
]
},
{
"Section" : "Title2",
"Items" :
[
{"Nr" : "102"},
{"Nr" : "202"},
{"Nr" : "302"}
]
}
]
向我展示所有包含 Imtes 且 Nr 以“20”开头的部分
我的问题是如何在项目中进行过滤?我需要的结果是:
result =
[
{
"Section" : "Title1",
"Items" :
[
{"Nr" : "201"}
]
},
{
"Section" : "Title2",
"Items" :
[
{"Nr" : "202"}
]
}
]
我尝试使用 SUBQUERY:
"SUBQUERY(Items, $x, $x.Nr BEGINSWITH[c] %@).@count > 0"
但这给我的回报是一样的,如果它可以计算项目,那么我认为它可以显示我找到的项目/编号,但我不知道如何正确地写这个。 :)
【问题讨论】:
标签: swift filter nsarray nspredicate