一 問題描述
查找數(shù)組中是否包含指定
二 問題解決
方法一
NSArray *array =@[@"abc123", @"cde234" , @"fgh456", @"ijk567", @"KLm567", @"MnO890", @"opq012", @"Rst123"];
//匹配字符串,反回結(jié)果, SELF==表示數(shù)組中每一個元素
NSString *match1 = @"MnO890";
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF == %@", match1];
NSArray *results1 = [array filteredArrayUsingPredicate:predicate1];
方法二
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isEqualToString:@"opq012"]) {
NSLog(@"%@-索引%d",obj, (int)idx);
}
}];
三 拓展延伸
如何使用謂詞NSPredicate實現(xiàn)快速尋找。
先看下定義
NSPredicate *ca = [NSPredicate predicateWithFormat:(NSString *), ...];
說明:
Format:有如下幾種規(guī)則
(1)比較運算符>,<,==,>=,<=,!=
可用于數(shù)值及字符串
例:@"number > 100"
(2)范圍運算符:IN、BETWEEN
例:@"number BETWEEN {1,5}"
@"address IN {'shanghai','beijing'}"
(3)字符串本身:SELF
例:@“SELF == ‘APPLE’"
(4)字符串相關(guān):BEGINSWITH、ENDSWITH、CONTAINS
例:@"name CONTAIN[cd] 'ang'" //包含某個字符串
@"name BEGINSWITH[c] 'sh'" //以某個字符串開頭
@"name ENDSWITH[d] 'ang'" //以某個字符串結(jié)束
注:[c]不區(qū)分大小寫[d]不區(qū)分發(fā)音符號即沒有重音符號[cd]既不區(qū)分大小寫,也不區(qū)分發(fā)音符號。
(5)通配符:LIKE
例:@"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd].
@"name LIKE[cd] '???er*'"
(6)正則表達(dá)式:MATCHES
例:NSString *regex = @"^A.+e$"; //以A開頭,e結(jié)尾
@"name MATCHES %@",regex"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
具體使用方法如下
1.NSString對象
NSArray *array =@[@"abc123", @"cde234" , @"fgh456", @"ijk567", @"KLm567", @"MnO890", @"opq012", @"Rst123"];
//匹配字符串,反回結(jié)果, SELF==表示數(shù)組中每一個元素
NSString *match1 = @"MnO890";
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF == %@", match1];
NSArray *results1 = [array filteredArrayUsingPredicate:predicate1];
//近似匹配字符串,類似SQL中的語法
NSString *match2 = @"fgh45";
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF contains %@", match2];
NSArray *results2 = [array filteredArrayUsingPredicate:predicate2];
//不區(qū)分大小寫匹配
NSString *match3 = @"klm567";
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match3];
NSArray *results3 = [array filteredArrayUsingPredicate:predicate3];
//正則匹配
NSString *match4 = @"[a-zA-Z0-9]{6}";
NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"SELF matches %@", match4];
NSArray *results4 = [array filteredArrayUsingPredicate:predicate4];
NSLog(@"%@\n%@\n%@\n%@\n",results1,results2,results3,results4);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
輸出結(jié)果
(
MnO890
)
(
fgh456
)
(
KLm567
)
(
abc123,
cde234,
fgh456,
ijk567,
KLm567,
MnO890,
opq012,
Rst123
)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
2.含有屬性的對象
@interface Person: NSObject
{
NSString *_name;
NSString *_telephone;
NSInteger _id;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *telephone;
@property (nonatomic, assign) NSInteger id;
@end
1).等于查詢
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", "張三"];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
2).模糊查詢
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"A"]; //predicate只能是對象
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];
|