Last active
January 9, 2016 03:46
-
-
Save steveatinfincia/432205828cb9aa8c994d to your computer and use it in GitHub Desktop.
swift nspredicate example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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