-
Re: NSArray filtered type
goldsdad Oct 24, 2016 10:57 PM (in response to Alexandre Carmo)NSArray has filtered(using:) method, but your Swift 3 code is explicitly declaring data to be a Swift Array, which has a filter(_:) method (and that takes a closure, not a NSPredicate, so see API reference for Array).
-
Re: NSArray filtered type
ahltorp Oct 25, 2016 1:36 AM (in response to Alexandre Carmo)If you still want to use NSPredicate, you will have to cast it to NSArray and cast the result back again.
self.filtered = (self.data as NSArray).filtered(using: resultPredicate) as! [[String:Any]]
If you are using this in many places, you could extend Array to have the .filtered(predicate:) method:
extension Array { func filtered(using predicate: NSPredicate) -> Array { return (self as NSArray).filtered(using: predicate) as! Array } }
If you have no particular reason to use NSPredicate, you are probably better off using .filter(_:) as goldsdad suggested.
-
Re: NSArray filtered type
eskimo Oct 25, 2016 4:13 AM (in response to ahltorp)If you have no particular reason to use NSPredicate, you are probably better off using
.filter(_:)
as goldsdad suggested.Agreed. For example:
filtered = data.filter { element in return (element["name_friend"] as? String)?.range(of: text, options: [.caseInsensitive]) != nil }
ps This code would be a lot nicer if you replaced the elements of the array with a model object rather than a dictionary. Most of the goo in the snippet above is related to extracting the
name_friend
‘field’ from the dictionary. If this were a struct, the code would be much simpler:filtered = data.filter { element in return element.nameFriend.range(of: text, options: [.caseInsensitive]) != nil }
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardwarelet myEmail = "eskimo" + "1" + "@apple.com"
-