IOS NSArray array filtering and iosnsarray array Filtering

Source: Internet
Author: User

IOS NSArray array filtering and iosnsarray array Filtering

 

Requirement: In an array, filter out the elements in this array and in another array.

That is, in an array dataArray, the elements in the dataArray array and in the filteredArray are filtered out.

// IOS-filter elements in the array // In dataArray, filter out the elements in the dataArray and in the filteredArray // The element array NSMutableArray * filteredArray = [[NSMutableArray alloc] initWithObjects: @ "1 ", @ "3", @ "9", nil]; // NSMutableArray * dataArray = [[NSMutableArray alloc] initWithObjects: @ "1 ", @ "2", @ "3", @ "1", @ "1", @ "3", @ "1", @ "5", @ "7 ", nil];/* Method 1: Using NSPredicate Note: NSPredicate's Cocoa framework is often used in regular expressions such as passwords and usernames. Similar to the SQL statement NOT, which is NOT SELF, which represents the IN range operator of the string itself, NOT (SELF IN % @) means: NOT the value of the specified string */NSPredicate * filterPredicate = [NSPredicate predicateWithFormat: @ "NOT (self in % @)", filteredArray]; // Filter Array NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate: filterPredicate]; NSLog (@ "Reslut Filtered Array = % @", reslutFilteredArray);/* Method 2: traverse the array from the back, and then match and delete */int I = (int) [dataArray count]-1; for (; I> = 0; I --) {// containsObject: determines whether an element exists in an array (based on the memory address of the two, the same: YES is different: NO) if ([filteredArray containsObject: [dataArray objectAtIndex: I]) {[dataArray removeObjectAtIndex: I] ;}} NSLog (@ "Data Array = % @", dataArray );

 

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.