• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-C(十八、谓语使用及实例说明)——iOS开发基础

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

结合之前的学习笔记以及參考《Objective-C编程全解(第三版)》,对Objective-C知识点进行梳理总结。知识点一直在变,仅仅是作为參考,以苹果官方文档为准~


十八、谓语的使用及实例说明


首先先介绍基本经常使用的谓词:
(1)逻辑运算符 && AND || OR 都能够用
(2)IN包括
(3)模糊查询
a、以……开头 BEGINSWITH
b、以……结尾 ENDSWITH
c、包括….字符 CONTAINS
(4)用like进行模糊查询
通配符:*表示随意个字符 ?表示单个字符

like *aa结尾
like a*  以a开头
like *a* 包括a字符
like ?a* 第二个字符为a的字符串

实例说明:


创建Book类,Book.h

@interface Book : NSObject
{
    NSInteger _price;
    NSString* _bookName;
}

- (instancetype)initWithPrice:(NSInteger)price andBookName:(NSString *)bookName;

@end

Book.h

#import "Book.h"

@implementation Book

- (instancetype)initWithPrice:(NSInteger)price andBookName:(NSString *)bookName {
    if (self = [super init]) {
        _price = price;
        _bookName = bookName;
    }
    return self;
}

- (NSString *)description {

    return [NSString stringWithFormat:@"Book price:%li,named %@",_price,_bookName];
}

@end

main.m

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Book* book1 = [[Book alloc] initWithPrice:20 andBookName:@"C Programming"];
        Book* book2 = [[Book alloc] initWithPrice:32 andBookName:@"C++ Programming"];
        Book* book3 = [[Book alloc] initWithPrice:18 andBookName:@"Java Programming"];
        Book* book4 = [[Book alloc] initWithPrice:45 andBookName:@"OC guiding"];
        Book* book5 = [[Book alloc] initWithPrice:28 andBookName:@"iOS guiding"];
        NSArray* books = [NSArray arrayWithObjects:book1,book2,book3,book4,book5, nil];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"price > %i",30];
        NSArray *filterArray = [books filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",filterArray);

//      逻辑运算符 和 IN
        predicate = [NSPredicate predicateWithFormat:@"bookName IN {'C Programming','C++ Programming'} AND price > 30"];
        filterArray = [books filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",filterArray);

//      模糊查询 和 用通配符查询

        predicate = [NSPredicate predicateWithFormat:@"bookName CONTAINS 'guiding' || bookName like '*Program*' "]; //包括guiding或者包括Program
        filterArray = [books filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",filterArray);



    }
    return 0;
}

output:

2015-07-09 20:17:24.403 exercise_谓语[632:9877] (
    "Book price:32,named C++ Programming",
    "Book price:45,named OC guiding"
)
2015-07-09 20:17:24.404 exercise_谓语[632:9877] (
    "Book price:32,named C++ Programming"
)
2015-07-09 20:17:24.407 exercise_谓语[632:9877] (
    "Book price:20,named C Programming",
    "Book price:32,named C++ Programming",
    "Book price:18,named Java Programming",
    "Book price:45,named OC guiding",
    "Book price:28,named iOS guiding"
)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C中严谨的单例模式发布时间:2022-07-12
下一篇:
Objective-c单例模式的正确写法--用dispatch线程安全发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap