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 );