Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取
初始化的方式:
- NSPredicate(format:"")
用法
1.检索值是否存在一个范围区间里
var number2 = 0
var numberPre2 = NSPredicate(format: "SELF BETWEEN{1,5}")
var bool2 = numberPre2.evaluateWithObject(number2)
打印结果
当number2 = 0 打印:bool2 为false
当number2 = 1 打印:bool2 为true
2.检索数组元素中是否包含给定字符串
var number2 = "dd"
var numberPre2 = NSPredicate(format: "SELF IN {'dd','bb'}")
var bool2 = numberPre2.evaluateWithObject(number2)
其中出现的关键字
(BETWEEN、IN)称为范围运算符
SELF表示的是字符串本身,在本例代码中相当于变量number2
当然以数组的形式初始化NSPredicate也是可以的
var sets = ["dd","ss"]
var numberPre2 = NSPredicate(format: "SELF IN %@",sets)
3.字符串操作相关
1>判断字符串中是否包含另一个字符串CONTAINS
var str3 = "dd"
var pre3 = NSPredicate(format: "SELF CONTAINS[cd] %@", str3)
var bool3 = pre3.evaluateWithObject("dsadd")
打印结果
当str3="da" 打印:bool3=false
当str3="dd" 打印:bool3=true
2>判断是否以某个字符串开头BEGINSWITH
var str3 = "dd"
var pre3 = NSPredicate(format: "SELF BEGINSWITH[cd] %@", str3)
var bool3 = pre3.evaluateWithObject("ddacd")
打印结果
当str3="da" 打印:bool3=false
当str3="dd" 打印:bool3=true
3>判断是否以某个字符串结尾ENDSWITH
var str3 = "dd"
var pre3 = NSPredicate(format: "SELF ENDSWITH[cd] %@", str3)
var bool3 = pre3.evaluateWithObject("dcadd")
打印结果
当str3="da" 打印:bool3=false
当str3="dd" 打印:bool3=true
注:
[c]不区分大小写
[d]不区分发音符号即没有重印符号
[cd]表示即不区分大小写,也不区分发音符号
4.模糊查询LINK通配符
5.正则表达式MATCHES
//匹配纯数字
var number = "^[0-9]+$"
var numberPre = NSPredicate(format: "SELF MATCHES %@", number)
var boo3 = numberPre.evaluateWithObject("13140000000")
打印结果为 true
6.基本操作符运算
let predicate = NSPredicate(format: "(SELF > 20) AND (SELF < 30)")
let boo7 = predicate.evaluateWithObject(num)
print(boo7)
打印结果
当num = 20 打印:false
当num = 21 打印:true
实际应用
1.检索一个数组中所有包含此字符串的元素,并返回一个结果的数组
let arr4 = ["beijing" , "shanghai" , "guangzhou" , "wuhan"] asNSMutableArray
let str4 = "ang"
let pre4 = NSPredicate(format: "SELF CONtAINS %@", str4)
arr4.filterUsingPredicate(pre4)
打印arr4 结果为
["shanghai","guangzhou"]
2.判断字符串首字符是否为字母
let regex1 = "[A-Za-z]+"
let pre5 = NSPredicate(format: "SELF MATCHES %@" , regex1)
let bool5 = pre5.evaluateWithObject("2")
3.字符串替换
//方法调用需要做异常处理
do{
let pattern = "(encoding=\")[^\"]+(\")"
let regex = try NSRegularExpression(pattern:
pattern , options:NSRegularExpressionOptions.CaseInsensitive)
let sample = "<xml encoding=\"abc\"></xml><xml
encoding=\"def\"></xml><xml encoding=\"ttt\"></xml>"
print(sample)
let result = regex.stringByReplacingMatchesInString(sample, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0, sample.characters.count), withTemplate: "$1utf-8$2")
print(result)
}catch{
print(error)
}
打印结果为
<xml encoding="abc"></xml><xml encoding="def"></xml><xml encoding="ttt"></xml>
<xml encoding="utf-8"></xml><xml encoding="utf-8"></xml><xml encoding="utf-8"></xml>
4.提取字符串
//组装一个字符串,需要把里面的网址解析出来
//方法调用需要做异常处理
do{
let urlString = "<meta/><link/><title>1Q84BOOK1</title></head><body>"
let pattern = "(?<=title\\>).*(?=</title)"
let regex = try NSRegularExpression(pattern: pattern , options: NSRegularExpressionOptions.CaseInsensitive)
let firstMatch = regex.firstMatchInString(urlString, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0, urlString.characters.count))
if firstMatch != nil {
let resultRange = firstMatch?.rangeAtIndex(0)
let result = (urlString as NSString).substringWithRange(resultRange!)
print(result)
}
}catch{
print(error)
}
打印结果
"1Q84BOOK1"