OC-简单的介绍谓词和使用方法

 /*NSPredicate 指定条件过滤数据*/
    
    
    Person *person1 = [[Person alloc] initPerson:@"张三" age:12];
    Person *person2 = [[Person alloc] initPerson:@"李四" age:21];
    Person *person3 = [[Person alloc] initPerson:@"王五" age:33];
    Person *person4 = [[Person alloc] initPerson:@"赵六" age:15];
    
    NSArray *storage = [NSArray arrayWithObjects:person1,person2,person3,person4, nil];
    
    //赛选满足谓词的条件返回是数组 满足年龄大于15
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"age>=%d",15]]);
    //满足姓名
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name=%@",@"张三"]]);
    //或预算符
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name=%@ or age=%d",@"张三",15]]);
    //in运算符
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name in {'张三','李四','赵六'}"]]);
    //某个字符开头
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name beginswith '张'"]]);
    //某个字符结尾
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name endswith '三'"]]);
    //包含字符
    NSLog(@"%@",[storage filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.name contains '三'"]]);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章