Simple use of NSPredicate in query, nspredicate Query

Source: Internet
Author: User

Simple use of NSPredicate in query, nspredicate Query

Run the code. Each piece of code can be run directly with comments and running results.

// Directly use = to strictly match NSArray * arrOriginal = [NSArray arrayWithObjects: @ "c1", @ "c", @ "cd", @ "cdd ", @ "cd", nil]; NSString * match = @ "cd"; NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "SELF =%@", match]; NSArray * results = [arrOriginal filteredArrayUsingPredicate: predicate]; NSLog (@ "result = % @", results );

Print result:
Result = (
Cd,
Cd
)

// Like match, similar to the usage in SQL NSArray * arrOriginal = [NSArray arrayWithObjects: @ "c1", @ "d", @ "cd", @ "cdd ", @ "dd", @ "dcd", @ "cdc", nil]; NSString * match = @ "c *"; // NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "SELF like % @", match]; NSArray * results = [arrOriginal filteredArrayUsingPredicate: predicate]; NSLog (@ "result = % @", results );

Print result:
Result = (
C1,
Cd,
Cdd,
Cdc
)

// Match NSArray with case-insensitive characters * arrOriginal = [NSArray arrayWithObjects: @ "Cdd", @ "cDc", @ "cdd", @ "dcdd", nil]; NSString * match = @ "cd *"; NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "SELF like [c] % @", match]; NSArray * results = [arrOriginal response: predicate]; NSLog (@ "result = % @", results );

Print result:
Result = (
Cdd,
CDc,
Cdd
)

// Use regular expression to filter NSArray * arrOriginal = [NSArray arrayWithObjects: @ "c1", @ "C3", @ "d2", @ "c2", nil]; NSString * match = @ "^ c \ d? "; NSPredicate * predicate = [NSPredicate predicateWithFormat: @" SELF matches % @ ", match]; NSArray * results = [arrOriginal filteredArrayUsingPredicate: predicate]; NSLog (@ "result = % @", results );

Print result:
Result = (
C1,
C2
)

// Filter NSArray * arrFilter = [NSArray arrayWithObjects: @ "aa", @ "bb", nil]; NSArray * arrOriginal = [NSArray arrayWithObjects: @ "dds", @ "bb", @ "c1", @ "C3", @ "bb", @ "aa", @ "c2", nil]; NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "NOT (SELF in % @)", arrFilter]; // NSArray * results = [arrOriginal attributes: predicate]; NSLog (@ "result = % @", results );

Print result:
Result = (
Dds,
C1,
C3,
C2
)


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.