BoSoud BoSoud - 7 months ago 26
Swift Question

Filtering dictionary inside an array in Swift

I have An Array With Dictionary example :

[{
"CATEGORYNAME" = "name0";
"CATEGORYSUBID" = 2;
"ID" = 1;
}, {
"CATEGORYNAME" = "name1";
"CATEGORYSUBID" = 2;
"ID" = 2;
}, {
"CATEGORYNAME" = "name2";
"CATEGORYSUBID" = 0;
"ID" = 3;
}]


I Used to Filter it in Objective C Like this

JSON_data = [[[Global SharedData]Categorys] filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(CATEGORYSUBID == %@)", Filter]];


i tried To Use Array Filter but not Succeed

var JSON_data = Global.SharedData().Categorys
JSON_data = JSON_data.filter( ?????


JSON_data is has all data i have print it with Printin

Answer

This looks like just a simple question in how to translate. Your best option is to go through the various Sessions from WWDC, but a literal translation is:

let categories = Global.SharedData().Categorys()
JSON_data = categories.filter({
    if let subid = $0["CATEGORYSUBID"] {
        return subid == filter
    } else {
        return false
    }
})

If that doesn't work you'll need to post a lot more information about how Global, SharedData, Categorys and JSON_data.