OBJECTIVE-C (18, predicate use and case description)--ios Development Foundation

Source: Internet
Author: User

Combining the previous study notes and referring to the full solution of Objective-c Programming (third edition), this paper summarizes the objective-c knowledge points. Knowledge points have been changing, just as a reference to the official Apple document ~

18, the use of predicates and examples of explanation

First, we introduce the basic commonly used predicates:
(1) Logical operators && and | | OR both can be used
(2) in contains
(3) Fuzzy query
A. A. Opening Beginswith
B. To ... End ENDSWITH
C, include .... Character CONTAINS
(4) Fuzzy query with like
Wildcard: * denotes any character? Represents a single character

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

Example Description:

Create book class, 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

intMainintargcConst Char* argv[]) {@autoreleasepool {book* book1 = [[Book Alloc] Initwithprice: -andbookname:@"C Programming"]; book* Book2 = [[Book Alloc] Initwithprice: +andbookname:@"C + + programming"]; book* BOOK3 = [[Book Alloc] Initwithprice: -andbookname:@"Java Programming"]; book* BOOK4 = [[Book Alloc] Initwithprice: $andbookname:@"OC guiding"]; book* Book5 = [[Book Alloc] Initwithprice: -andbookname:@"IOS guiding"];Nsarray* Books = [NsarrayArraywithobjects:book1,book2,book3,book4,book5,Nil]; Nspredicate *predicate = [Nspredicate predicatewithformat:@"Price >%i", -];Nsarray*filterarray = [Books filteredarrayusingpredicate:predicate];NSLog(@"%@", Filterarray);//logical operators and inpredicate = [Nspredicate predicatewithformat:@"BookName in {' C programming ', ' C + + programming '} and Price >"]; Filterarray = [Books filteredarrayusingpredicate:predicate];NSLog(@"%@", Filterarray);//Fuzzy query and query with wildcard characterspredicate = [Nspredicate predicatewithformat:@"BookName CONTAINS ' guiding ' | | BookName like ' *program* ' "];//contains guiding or contains programFilterarray = [Books filteredarrayusingpredicate:predicate];NSLog(@"%@", Filterarray); }return 0;}

Output

2015-07-0920::. 403 Exercise_Predicate[632:9877](    " Book  Price:,named C++Programming",    " Book  Price:,named OC guiding") 2015-07-0920::. 404 Exercise_Predicate[632:9877](    " Book  Price:,named C++Programming") 2015-07-0920::. 407 Exercise_Predicate[632:9877](    " Book  Price:,named C Programming",    " Book  Price:,named C++Programming",    " Book  Price:,named Java Programming",    " Book  Price:,named OC guiding",    " Book  Price:,named IOS guiding")

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OBJECTIVE-C (18, predicate use and case description)--ios Development Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.