Advertisement
Larme

Untitled

Feb 24th, 2021
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.52 KB | None | 0 0
  1. class Player: NSObject {
  2.     @objc var attributes: [String]
  3.     @objc var name: String
  4.  
  5.     init(name: String, attributes: [String]? = nil) {
  6.         self.attributes = attributes ?? name.components(separatedBy: "")
  7.         self.name = name
  8.     }
  9.  
  10.     override var description: String {
  11.         return "(P: \(name) - [\(attributes.joined(separator: ","))])"
  12.     }
  13. }
  14.  
  15. let players = [Player(name: "ABC"), Player(name: "BCD"), Player(name: "CDE"), Player(name: "DEF")]
  16. print(players)
  17.  
  18. // Sample init test
  19. let filtered1 = players.filter {
  20.     NSPredicate(format: "name CONTAINS %@", "B").evaluate(with: $0)
  21. }
  22. //(to check that predicate is working, not key value non compliant issue.
  23. print("filtered1: \(filtered1)")
  24.  
  25. // ANY
  26. let filtered2 = players.filter {
  27.     NSPredicate(format: "ANY attributes CONTAINS %@", "B").evaluate(with: $0)
  28. }
  29. print("filtered2: \(filtered2)")
  30.  
  31. let filtered3 = players.filter {
  32.     NSPredicate(format: "NOT (ANY attributes CONTAINS %@)", "B").evaluate(with: $0)
  33. }
  34. print("filtered3: \(filtered3)")
  35.  
  36.  
  37. // Without ANY
  38. let filtered2AndHalf = players.filter {
  39.     NSPredicate(format: " attributes CONTAINS %@", "B").evaluate(with: $0)
  40. }
  41. print("filtered2AndHalf: \(filtered2AndHalf)") //Empty because non will be equal
  42. let filtered3AndHalf = players.filter {
  43.     NSPredicate(format: "NOT (attributes CONTAINS %@)", "B").evaluate(with: $0)
  44. }
  45. 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