Advanced Application of nsarray in iOS-filter.

Source: Internet
Author: User

 

C/C ++ developers must like to use the boost library, which has powerful container functions. In iOS development, array is also equivalent to a container and has powerful functions. Nsarray can use nspredicate to filter the array content. nspredicate is a bit like SQL + regular expression. The specific syntax is not mentioned here. I will Google it myself.

 

I don't want to type (I'm super lazy). Use the code to speak:

View plaincopy to clipboardprint?
  1. // Find an array
  2. Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
  3. Nsstring * defaultpath = [[nsbundle mainbundle] resourcepath];
  4. Nserror * error;
  5. Nsarray * directorycontents = [filemanager contentsofdirectoryatpath: defaultpath error: & error]
  6. // Matches the string and returns the reverse result. Self = indicates each element in the array.
  7. Nsstring * match = @ "imagexyz-999.png ";
  8. Nspredicate * predicate = [nspredicate predicatewithformat: @ "Self = % @", Match];
  9. Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];
  10. // Approximate match string, similar to the syntax in SQL
  11. Nsstring * match = @ "imagexyz *. PNG ";
  12. Nspredicate * predicate = [nspredicate predicatewithformat: @ "self like % @", Match];
  13. Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];
  14. // Case-insensitive matching
  15. Nsstring * match = @ "imagexyz *. PNG ";
  16. Nspredicate * predicate = [nspredicate predicatewithformat: @ "self like [cd] % @", Match];
  17. Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];
  18. // Regular Expression matching
  19. Nsstring * match = @ "imagexyz-// d {3} //. PNG ";
  20. Nspredicate * predicate = [nspredicate predicatewithformat: @ "Self matches % @", Match];
  21. Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];

Related Article

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.