swift - NSPredicate Filtered childs in array -


good day, i'm using new language apple swift , using 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"}         ]     } ] 

i want use search, , display content found

as understand have use "filteredarrayusingpredicate" , nspredicate in swift, sample is:

var arr:nsarray = [...] // array sample goes here var pre:nspredicate = nspredicate(format: "any items.nr beginswith[c] %@", argumentarray: ["20"]) // going search "20" in /items/nr  var result:nsarray = arr.filteredarrayusingpredicate(pre) 

this working correct result not need:

result =  [     {         "section" : "title1",         "items" :         [             {"nr" : "101"},             {"nr" : "201"},             {"nr" : "301"},             {"nr" : "401"}         ]     },     {         "section" : "title2",         "items" :         [             {"nr" : "102"},             {"nr" : "202"},             {"nr" : "302"}         ]     } ] 

it display me section contains imtes nr started "20"

and question how filter in items? result need have be:

result =  [     {         "section" : "title1",         "items" :         [             {"nr" : "201"}         ]     },     {         "section" : "title2",         "items" :         [             {"nr" : "202"}         ]     } ] 

i tried use subquery:

"subquery(items, $x, $x.nr beginswith[c] %@).@count > 0" 

but return me same, if can calculate items, thought can display me items/nr found, dont know how write correct. :)

oh, hang on. re-read question, editing now

to answer requirement of filtering sub arrays. data model getting far complex still using generic objects. should think setting actual data structure custom objects. can offload lot of work these objects instead of putting work in 1 place.

just trying find best way (without custom data model).

p.s. make custom data model :)

ok, here go

i made lot easier wrapping generic data struct...

struct item {     let section: string     let items: [[string: string]] } 

it still feels wrong having array of dictionaries each dictionary has 1 key , it's same. should array of strings.

anyway...

create array so...

let items: [item] = [     item(section: "title1", items: [["nr" : "101"], ["nr" : "201"], ["nr" : "301"], ["nr" : "401"]]),     item(section: "title2", items: [["nr" : "102"], ["nr" : "202"], ["nr" : "302"]]),     item(section: "title3", items: [["nr" : "1102"], ["nr" : "2102"], ["nr" : "3102"]]) ] 

or data have stored.

then filter , map new array...

let filteredmappedarray = items.filter {     // filters array containing "20..." in it.     dictionary in $0.items {         if let numberstring = dictionary["nr"] {             if (numberstring.hasprefix("20")) {                 return true             }         }     }     return false }.map {     // maps array , removes isn't "20..." items.     item(section: $0.section, items: $0.items.filter {         numberdictionary in         if let numberstring = numberdictionary["nr"] {             return numberstring.hasprefix("20")         }         return false     }) } 

then logged result...

for item in filteredmappedarray {     println("section: \(item.section) - items: \(item.items)") } 

and got result...

section: title1 - items: [[nr: 201]] section: title2 - items: [[nr: 202]] 

there may better/simpler way combine 1 function easiest way find.

if can change dictionary array thing...

if item can defined as...

struct item {     let section: string     let items: [string] } 

then filter-map function become...

let filteredmappedarray = items.filter {     // filters array containing "20..." in it.     numberstring in $0.items {         if (numberstring.hasprefix("20")) {             return true         }     }     return false }.map {     // maps array , removes isn't "20..." items.     item(section: $0.section, items: $0.items.filter {$0.hasprefix("20")}) } 

by changing dictionary array array of strings you're removing layer of complexity isn't necessary.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -