You are having problems with a filter containing dictionaries coming from the server in fast

Dictionary coming from the server,

{
    data = ({
        email = "a123@gmail.com";
        phone = 9804504884;
        "user_id" = 11;
        username = abcd;
    });
}

var dataArray:NSArray = dict.objectForKey("data") as! NSArray
println("names = ,\(dataArray)");
var pre:NSPredicate = NSPredicate(format: "username CONTAINS[c] a")
var result:NSArray = dataArray.filteredArrayUsingPredicate(pre)
println("names = ,\(result)");

I always get an array of results from pure code. Please help me solve this problem. Thanks

+4
source share
1 answer

Do it like that

let json = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
let predicate = NSPredicate(format: "username CONTAINS[C] 'a'")
if let filteredArray = json["data"]?.filteredArrayUsingPredicate(predicate) {
   // do something with array
}

Note that you must wrap your string in single quotes. ''

+4
source

All Articles