Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player: NSObject {
- @objc var attributes: [String]
- @objc var name: String
- init(name: String, attributes: [String]? = nil) {
- self.attributes = attributes ?? name.components(separatedBy: "")
- self.name = name
- }
- override var description: String {
- return "(P: \(name) - [\(attributes.joined(separator: ","))])"
- }
- }
- let players = [Player(name: "ABC"), Player(name: "BCD"), Player(name: "CDE"), Player(name: "DEF")]
- print(players)
- // Sample init test
- let filtered1 = players.filter {
- NSPredicate(format: "name CONTAINS %@", "B").evaluate(with: $0)
- }
- //(to check that predicate is working, not key value non compliant issue.
- print("filtered1: \(filtered1)")
- // ANY
- let filtered2 = players.filter {
- NSPredicate(format: "ANY attributes CONTAINS %@", "B").evaluate(with: $0)
- }
- print("filtered2: \(filtered2)")
- let filtered3 = players.filter {
- NSPredicate(format: "NOT (ANY attributes CONTAINS %@)", "B").evaluate(with: $0)
- }
- print("filtered3: \(filtered3)")
- // Without ANY
- let filtered2AndHalf = players.filter {
- NSPredicate(format: " attributes CONTAINS %@", "B").evaluate(with: $0)
- }
- print("filtered2AndHalf: \(filtered2AndHalf)") //Empty because non will be equal
- let filtered3AndHalf = players.filter {
- NSPredicate(format: "NOT (attributes CONTAINS %@)", "B").evaluate(with: $0)
- }
- print("filtered3AndHalf: \(filtered3AndHalf)") //Will return everying as expected, since it's the reverse of which returns nothing, it returns then all.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement