美文网首页
iOS 中的特殊类

iOS 中的特殊类

作者: iChuck | 来源:发表于2018-03-20 10:21 被阅读8次

    NSPredicate

    • NSPredicate 是预测的意思,但我们常翻译成谓词。
    • 使用NSPredicate可以定义模糊查找条件,根据一定的条件从一个数组中快速找出符合一定条件的元素对象。
    • 可以直接搜索对象
    • 可以使用 >,<,==,>=,<=,!=来进行判断数据
    @interface Person : NSObject
    
    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, assign) int age;
    
    @end
    
    self.array = [[NSMutableArray alloc]init];
        
    for (int i = 0; i < 10; i++) {
        
        Person *p = [[Person alloc] init];
        p.name = [NSString stringWithFormat:@"Mr.chen %d", i];
        p.age = i * 10;
        [self.array addObject:p];
    }
        
    NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"age>50"];
    NSArray *arr  = [self.array filteredArrayUsingPredicate:perdicate];
    NSLog(@"%@",arr);
    
    // 打印结果
    (
        "name:Mr.chen 6, age:60",
        "name:Mr.chen 7, age:70",
        "name:Mr.chen 8, age:80",
        "name:Mr.chen 9, age:90"
    )
    
    
    NSArray *arr1 = @[@1, @2, @3, @43, @6, @60];
    
    
    NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"SELF == 6"];
    NSArray *temp = [arr1 filteredArrayUsingPredicate:perdicate];
    NSLog(@"%@", temp);
    
    //打印结果
    (
        6
    )
    
    
    • 可以使用 SELF 关键字代表自己
    • in: arr1 和 arr2的交集
    NSArray *arr1 = @[@1, @2, @3, @43, @6];
    NSArray *arr2 = @[@2, @6];
        
    NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"SELF in %@", arr2];
    NSArray *temp = [arr1 filteredArrayUsingPredicate:perdicate];
    NSLog(@"%@", temp);
    
    // 打印结果
    (
        2,
        6
    )
    
    • BETWEEN 获取一定范围的值
    NSArray *arr1 = @[@1, @2, @3, @43, @6, @60];
    
    NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN{1,20}"];
    NSArray *temp = [arr1 filteredArrayUsingPredicate:perdicate];
    NSLog(@"%@", temp);
    
    // 打印结果
    (
        1,
        2,
        3,
        6
    )
    
    
    • BEGINSWITH 以某个字符串开头
    • ENDSWITH 以某个字符串结尾
    • CONTAINS 是否包含某个字符串
    NSArray *arr1 = @[@"zhang3", @"wang4", @"lihu"];
    
    NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'z' "];
    NSArray *temp = [arr1 filteredArrayUsingPredicate:perdicate];
    NSLog(@"%@", temp);
    
    // 打印结果
    (
        zhang3
    )
    
    
    • 还可以使用LIKE它后面可以写[cd]格式符号, * 表示通配符
    NSArray *arr1 = @[@"zhang3", @"wang4", @"lihu"];
    
    NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] '*n*' "];
    NSArray *temp = [arr1 filteredArrayUsingPredicate:perdicate];
    NSLog(@"%@", temp);
    
    // 打印结果
    (
        zhang3,
        wang4
    )
    

    NSSortDescriptor

    • NSSortDescriptor 我们可以用它来排序
    self.array = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < 12; i++) {
    
        Person *p = [[Person alloc] init];
        p.name = [NSString stringWithFormat:@"Mr.chen %d", i];
        if (i > 9) {
            p.age = 20;
        } else {
            p.age = arc4random_uniform(100);
        }
        p.money = arc4random_uniform(1000000000) * arc4random_uniform(1000000000) / 9 / 7;
        [self.array addObject:p];
    }
        
    NSLog(@"%@",self.array);
    
    NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
    NSSortDescriptor *moneySD = [NSSortDescriptor sortDescriptorWithKey:@"money" ascending:NO];
        
    NSArray *arr = [self.array sortedArrayUsingDescriptors:@[ageSD, moneySD]];
        
    NSLog(@"%@",arr);
    
    // 打印结果
    
    self.array = (
        "name : Mr.chen 0, age : 38 , money : 39527952.00",
        "name : Mr.chen 1, age : 6 , money : 44344428.00",
        "name : Mr.chen 2, age : 39 , money : 37546996.00",
        "name : Mr.chen 3, age : 31 , money : 42419204.00",
        "name : Mr.chen 4, age : 51 , money : 37637156.00",
        "name : Mr.chen 5, age : 28 , money : 48236272.00",
        "name : Mr.chen 6, age : 80 , money : 29929636.00",
        "name : Mr.chen 7, age : 56 , money : 17334832.00",
        "name : Mr.chen 8, age : 1 , money : 53353300.00",
        "name : Mr.chen 9, age : 13 , money : 4750739.00",
        "name : Mr.chen 10, age : 20 , money : 60696080.00",
        "name : Mr.chen 11, age : 20 , money : 39092136.00"
    )
    arr = (
        "name : Mr.chen 8, age : 1 , money : 53353300.00",
        "name : Mr.chen 1, age : 6 , money : 44344428.00",
        "name : Mr.chen 9, age : 13 , money : 4750739.00",
        "name : Mr.chen 10, age : 20 , money : 60696080.00",
        "name : Mr.chen 11, age : 20 , money : 39092136.00",
        "name : Mr.chen 5, age : 28 , money : 48236272.00",
        "name : Mr.chen 3, age : 31 , money : 42419204.00",
        "name : Mr.chen 0, age : 38 , money : 39527952.00",
        "name : Mr.chen 2, age : 39 , money : 37546996.00",
        "name : Mr.chen 4, age : 51 , money : 37637156.00",
        "name : Mr.chen 7, age : 56 , money : 17334832.00",
        "name : Mr.chen 6, age : 80 , money : 29929636.00"
    )
    
    • [NSSortDescriptor sortDescriptorWithKey:@"money" ascending:NO] 直接用内方法创建
    • ascending YES,为升序,NO 为降序
    • 当两个一起用的时候以第一个为基础的如果有相同在排序第二个,以此类推

    相关文章

      网友评论

          本文标题:iOS 中的特殊类

          本文链接:https://www.haomeiwen.com/subject/ccmpqftx.html