Skip to content

Instantly share code, notes, and snippets.

@steveatinfincia
Last active January 9, 2016 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveatinfincia/432205828cb9aa8c994d to your computer and use it in GitHub Desktop.
Save steveatinfincia/432205828cb9aa8c994d to your computer and use it in GitHub Desktop.
swift nspredicate example
func search(words: [String]?) -> [Codepoint] {
guard let searchTerms = words else {
print("failed to unpack words")
return []
}
var predicates : [NSPredicate] = [NSPredicate]()
for term in searchTerms {
let codePredicate = NSPredicate(format: "(%@ IN[cd] code)", term)
let typePredicate = NSPredicate(format: "(%@ IN[cd] type)", term)
let characterPredicate = NSPredicate(format: "(%@ IN[cd] character)", term)
let fullPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [codePredicate, typePredicate, characterPredicate])
predicates.append(fullPredicate)
}
let fullPredicates = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
let filteredArray = self.unicodeArray.filter {
fullPredicates.evaluateWithObject($0)
}
return filteredArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment