Search code examples
iosarraysjsonswift2anyobject

Filter AnyObject in Swift 2


I have an JsonArray named data which I pass to AnyObject:

 if let dtMenu: AnyObject = responseObject?.valueForKey("data") {
                   print(filteredMenu)
                 }

// I got JsonArray here // My data are

 "data":[
    {
    "MENUITEMID":1.0,
    "MENUITEMNAMEENG":"IGW",
    "MENUITEMHREF":"IGW_1",
    "MENUITEMTYPE":"R",
    "MENUITEMLEVEL":1.0,
    "MENUGRPID":0.0,
    "MENUGRPSERIAL":1.0
    },
    {
    "MENUITEMID":6.0,
    "MENUITEMNAMEENG":"Dashboard",
    "MENUITEMHREF":"Dashboard_IGW",
    "MENUITEMTYPE":"L",
    "MENUITEMLEVEL":2.0,
    "MENUGRPID":1.0,
    "MENUGRPSERIAL":1.0
    }]

//I want to filter array by MENUITEMTYPE=R

Please help..


Solution

  • Try this.

        var predicate = NSPredicate(format: "%K == %@", "MENUITEMTYPE", "R")
    
        let filteredArray = yourArray.filter { predicate.evaluateWithObject($0) };
    

    I have not tested this yet.