Last active
February 7, 2019 11:56
-
-
Save sisoje/7a844e230ee775fae754e3df4527e5c8 to your computer and use it in GitHub Desktop.
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
struct Person { | |
let name: String | |
let age: Int | |
} | |
let persons = [ | |
Person(name: "Pera", age: 20), | |
Person(name: "Mika", age: 30), | |
Person(name: "Laza", age: 40) | |
] | |
// valid predicates | |
let namePredicate = NSPredicate(format: "name = %@", "Mika") | |
let agePredicate = NSPredicate(format: "age >= %@", 30) | |
let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [namePredicate, agePredicate]) | |
// compiles but fails in runtime - wrong type | |
let wrongPredicate1 = NSPredicate(format: "age >= %@", "text") | |
// compiles but fails in runtime - wrong property | |
let wrongPredicate2 = NSPredicate(format: "password == %@", "123") | |
// finds two: (Mika, 30) and (Laza, 40) | |
_ = persons.filtered(using: agePredicate) | |
// finds one: (Mika, 30) | |
_ = persons.filtered(using: compoundPredicate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment