NSPredicate 的使用(持续更新)

简介:

NSPredicate

谓词工具一般用于过滤数组数据,也可用来过滤CoreData查询出的数据.

1). 支持keypath

2). 支持正则表达式

 

在使用之前先新建3个类 Teacher Info Address,详细代码如下

Info.h

#import <Foundation/Foundation.h>

@interface Info : NSObject

@property (nonatomic, strong) NSString *classNum;

@end

Info.m
#import "Info.h"

@implementation Info

@end

Address.h
#import <Foundation/Foundation.h>

@interface Address : NSObject

@property (nonatomic, strong) NSString *detailAddress;

@end

Address.m
#import "Address.h"

@implementation Address

@end

Teacher.h
#import <Foundation/Foundation.h>
#import "Info.h"
#import "Address.h"

@interface Teacher : NSObject

@property (nonatomic, strong) NSString   *name;
@property (nonatomic, strong) Info       *info;
@property (nonatomic, strong) Address    *address;
@property (nonatomic, assign) NSInteger   age;

@end

Teacher.m
#import "Teacher.h"

@implementation Teacher

- (instancetype)init
{
    self = [super init];
    if (self) {
     //此处必须初始化以下对象
        _info    = [[Info alloc] init];
        _address = [[Address alloc] init];
    }
    return self;
}

@end

 

初始化数据并添加进数组中

//初始化数据
    Teacher *teacher1 = [[Teacher alloc] init];
    teacher1.info.classNum         = @"11班";
    teacher1.address.detailAddress = @"海淀区";
    teacher1.name                  = @"L.Y.F.";
    teacher1.age                   = 11;
    
    Teacher *teacher2 = [[Teacher alloc] init];
    teacher2.info.classNum         = @"12班";
    teacher2.address.detailAddress = @"立水桥";
    teacher2.name                  = @"P.K.";
    teacher2.age                   = 20;

    Teacher *teacher3 = [[Teacher alloc] init];
    teacher3.info.classNum         = @"11班";
    teacher3.address.detailAddress = @"万盛路";
    teacher3.name                  = @"Y.X.";
    teacher3.age                   = 22;
    
    //将数据添加进数组
    NSMutableArray *teachers =
        [[NSMutableArray alloc] initWithObjects:teacher1, teacher2, teacher3, nil];

开始正式的使用谓词

[1] 比较操作 (>,<,>=,<=,=)

[2] 字符串常规操作 (beginswith,endswith,contains)

    @"name beginswith[cd] 'Y'"
    @"name endswith[cd] 'X.'"
    @"name contains[cd] 'X'"

[3] 范围 (between,in)

    @"age between {10, 20}"
    @"age in {10, 20}" //这个不确定是什么

[4] 通配符 (like)

注:使用?表示一个字符,*表示多个字符

    @"name like[cd] '*X*'"

[5] 逻辑运算 (AND,OR,NOT)

@"age <= 22 AND name like[cd] '*X*'"

[6] 正则表达式

注:^Y.+.$ 以Y开头,以.结尾的字符

    @"self.name matches '^Y.+.$'"

[7] keypath

目录
相关文章
|
1月前
|
存储 程序员 C++
C++ 迭代器之旅(Journey of Iterators)
C++ 迭代器之旅(Journey of Iterators)
36 0
|
9月前
|
程序员
ACM刷题之路(一)第K个排列问题 Ignatius and the Princess II
ACM刷题之路(一)第K个排列问题 Ignatius and the Princess II
|
机器学习/深度学习 移动开发 JavaScript
iOS小技能:NSPredicate在正则表达式的应用【下篇】
应用案例: 商品分类名称(仅支持数字、字母、中文、斜杠\、横杠",且不能以符号开头)
105 0
iOS小技能:NSPredicate在正则表达式的应用【下篇】
|
Kubernetes Shell Perl
CKAD 8. Bonus Exercises考试必背
CKAD 8. Bonus Exercises考试必背
|
SQL 设计模式 Java
带你认识和使用Room|青训营笔记
Room 是 Google jetpack 体系的一个数据库框架,近年来 Google 力推该框架,作为开发者,我们也需拥抱新技术。
带你认识和使用Room|青训营笔记
|
机器学习/深度学习 编解码 数据挖掘
CVPR2020 论文和代码合集
CVPR2020 论文和代码合集
355 0
|
存储 算法 PHP
PHP数组学习系列大汇总(持续更新~)
这段时间写了很多关于PHP数组学习系列文章,相信或多或少都对大家有所帮助。那么今天这篇文章就来给大家将之前所有数组学习系列的文章进行一个大汇总,方便大家进行学习,欢迎大家收藏分享学习~ PHP数组学习系列文章汇总如下:
236 0