zoukankan      html  css  js  c++  java
  • NSPredicate的用法、数组去重、比较...

    一般来说这种情况还是蛮多的,比如你从文件中读入了一个array1,然后想把程序中的一个array2中符合array1中内容的元素过滤出来。

    1)例子一,一个循环

    NSArray *arrayFilter = [NSArray arrayWithObjects:@"pict", @"blackrain", @"ip", nil];

    NSArray *arrayContents = [NSArray arrayWithObjects:@"I am a picture.", @"I am a guy", @"I am gagaga", @"ipad", @"iphone", nil];

    我想过滤arrayContents的话只要循环 arrayFilter就好了

    int i = 0, count = [arrayFilter count];

    for(i = 0; i < count; i ++)

    {

    NSString *arrayItem = (NSString *)[arrayFilter objectAtIndex:i];

    NSPredicate *filterPredicate = [[NSPredicate predicateWithFormat:@"SELF CONTAINS %@", arrayItem];

    NSLog(@"Filtered array with filter %@, %@", arrayItem, [arrayContents filteredArrayUsingPredicate:filterPredicate]);

    }

    当然以上代码中arrayContent最好用mutable 的,这样就可以直接filter了,NSArray是不可修改的。

    2)例子二,无需循环

    NSArray *arrayFilter = [NSArray arrayWithObjects:@"abc1", @"abc2", nil];

    NSArray *arrayContent = [NSArray arrayWithObjects:@"a1", @"abc1", @"abc4", @"abc2", nil];

    NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter];

    [arrayContent filterUsingPredicate:thePredicate];

    这样arrayContent过滤出来的就是不包含 arrayFilter中的所有item了。

    3)生成文件路径下文件集合列表

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *defaultPath = [[NSBundle mainBundle] resourcePath];
    NSError *error;
    NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&error]


    4)match的用法

    1. 简单比较

    NSString *match = @"imagexyz-999.png";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];
    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

    2. match里like的用法(类似Sql中的用法)
    NSString *match = @"imagexyz*.png";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];

    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

    3. 大小写比较
    [c]表示忽略大小写,[d]表示忽略重音,可以在一起使用,如下:
    NSString *match = @"imagexyz*.png";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match];

    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];

    4.使用正则

    NSString *match = @"imagexyz-\d{3}\.png"; //imagexyz-123.png

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];

    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];


    总结:

    1) 当使用聚合类的操作符时是可以不需要循环的

    2)当使用单个比较类的操作符时可以一个循环来搞定

    PS,例子 一中尝试使用[@"SELF CONTAINS %@", arrayFilter] 来过滤会挂调,因为CONTAINS时字符串比较操作符,不是集合操作符。

    NSPredicate的用法  http://www.cnblogs.com/MarsGG/articles/1949239.html
  • 相关阅读:
    HDU 1556 Color the ball【树状数组】
    HDU 3015 Disharmony Trees 【 树状数组 】
    POJ 1990 MooFest【 树状数组 】
    codeforces 493 C Vasya and Basketball
    12、Decorator 装饰器 模式 装饰起来美美哒 结构型设计模式
    11、Composite 组合模式 容器与内容的一致性(抽象化) 结构型设计模式
    10、Strategy 策略模式 整体地替换算法 行为型模式
    9、Bridge 桥梁模式 将类的功能层次结构与实现层结构分离 结构型设计模式
    读源码从简单的集合类之ArrayList源码分析。正确认识ArrayList
    8、Builder 建造者模式 组装复杂的实例 创造型模式
  • 原文地址:https://www.cnblogs.com/yema/p/5052801.html
Copyright © 2011-2022 走看看