ios-filter elements within an array

Source: Internet
Author: User

    Array of elements to be removed nsmutablearray *filteredarray = [[Nsmutablearray alloc]initwithobjects:@ "1", @ "4", nil]; Array required to be filtered nsmutablearray *dataarray = [[Nsmutablearray alloc]initwithobjects:@ "1", @ "2", @ "1", @ "4", @ "6", @ "1", @ "1", @ "    4 ", @" 1 ", @" 6 ", @" 4 ", nil];                /* Method One: Use Nspredicate Note: Nspredicate belongs to the cocoa Framework, in the password, user name and other regular judgments used. Similar to the SQL statement not the self represents the string itself in the range operator then the not (itself in%@) means: Not the value of the string specified here */nspredicate * filterpredicate = [nspredicate predicatewithformat:@ ' not ' (self in%@) ', Filteredarray    ];    Filter array Nsarray * Reslutfilteredarray = [DataArray filteredarrayusingpredicate:filterpredicate];    NSLog (@ "Reslut Filtered Array =%@", Reslutfilteredarray); /* Results: Reslut Filtered array = (2, 6, 6)/*/* Method Two: Iterate through the array, then match delete */int    i = (int) [DataArray count]-1; for (; I >= 0;i-) {//containsobject Determines whether the element exists in the array, as determined by the memory address of the two: YES differs: NO) if ([Filteredarray Containsobject:[dataarray objectatindex:i]]) {[DataArray removeobjectatindex:i];    }} NSLog (@ "Data Array =%@", DataArray); /* Result: Data Array = (2, 6, 6) */

ios-filter elements within an array

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.