Explanation of iOS SDK NSPredicate (Swift)
Predicates are widely used in set filtering and CoreData. This article uses the Swift code on Playground as an example to explain how to use NSPredicate.
Preparations
Create an array on Playground to prepare for the predicate filter in the following text. Here we have congested the Description attribute to provide more obvious output.
import UIKitclass Person:NSObject { var name:String var age:Int init(name:String,age:Int) { self.name = name self.age = age super.init() } override var description:String{ return name:(self.name) age:(self.age) }}let persons = NSMutableArray()persons.addObject(Person(name: Jack Tomphon, age: 23))persons.addObject(Person(name: Mikle Steven, age: 25))persons.addObject(Person(name: Tacmk, age: 24))
Starting from an example
let pridicateByAge = NSPredicate(format: age == 24)let pridicateByAge2 = NSPredicate(format: age == %@,NSNumber(int: 24))let pridicateByAge3 = NSPredicate(format: %K == %@,age,NSNumber(int: 24))let pridicateByAge4 = NSPredicate (format: age == $age)let result = persons.filteredArrayUsingPredicate(pridicateByAge)let resule2 = persons.filteredArrayUsingPredicate(pridicateByAge2)let result3 = persons.filteredArrayUsingPredicate(pridicateByAge3)let result4 = persons.filteredArrayUsingPredicate(pridicateByAge4.predicateWithSubstitutionVariables([age:NSNumber(int: 24)]))
Then, let's look at the dynamic results provided by Playgound.
We can see that the effects of the above four methods are the same, so we will explain the syntax structure of the predicates.
Syntax structure usage
%@
It corresponds to numbers, strings, and date substitution values.
%K
Corresponds to the property to be compared, that is, the key Usage in KVC
$ Variable name
To indicate the configuration variables, and then
predicateWithSubstitutionVariables
To determine the specific variable value. Basic comparison
Comparison symbols are for the expression on the left and the expression on the right.
1.> greater
2.> = greater than or equal
3. <less
4. <= less than or equal
5. = equal
6 .! = Or <> not equal
7. BETWEEN is BETWEEN the two, including the upper and lower limits
For example
let pridivateByAge5 = NSPredicate(format: age BETWEEN { %@ , %@ }, NSNumber(int: 24),NSNumber(int: 25))let result5 = persons.filteredArrayUsingPredicate(pridivateByAge5)
Is to filter 24 <= age <= 25
Therefore, the result of Playground is:
[name:Mikle Steven age:25, name:Tacmk age:24]
Composite Comparison & or and logic | OR logic OR! Or the NOT logic is NOT
This is relatively simple. I will not give an example here.
String comparison inswith the left expression starts with the right expression. The left expression of CONTAINS the right expression. The left expression of ENDSWITH starts with the right expression. The left expression of LIKE is similar to the right expression (simple regular expression matching ,? Matches one character. * matches 0 or more characters) MATCHES can be used to implement more complex expressions. Previously, expression matching is performed using square brackets and cd to make it case insensitive and the expression on the left of the variable IN is IN the set on the right.
Match with a name starting with Ja
let pridivateByName1 = NSPredicate(format: name BEGINSWITH %@,Ja)let result6 = persons.filteredArrayUsingPredicate(pridivateByName1)println(result6)
The name contains ac, case insensitive, and age greater than or equal to 24
let pridivateByName2 = NSPredicate(format: name CONTAINS %@ && age >= %@, ac,NSNumber(int: 24))let result7 = persons.filteredArrayUsingPredicate(pridivateByName2)println(result7)
Composite Regular Expression T [a-z] * k
let privatedivateByName3 = NSPredicate(format: name MATCHES 'T[a-z]*k')let result8 = persons.filteredArrayUsingPredicate(privatedivateByName3)
The name is one of the two
let privatedivateByName4 = NSPredicate(format: name IN {'Tacmk','Jack Tomphon'})let result9 = persons.filteredArrayUsingPredicate(privatedivateByName4)
Block-based predicates
Note: Blcok-based predicates cannot be used for CoreData data filtering. You can customize predicates flexibly Based on blocks.
Simple Block definition age> 24
let blockPredicate = NSPredicate { (person: AnyObject!, [NSObject : AnyObject]!) -> Bool in var result = false if let castResult = person as? Person{ if castResult.age > 24{ result = true } } return result}let result10 = persons.filteredArrayUsingPredicate(blockPredicate)