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?
- // Find an array
- Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
- Nsstring * defaultpath = [[nsbundle mainbundle] resourcepath];
- Nserror * error;
- Nsarray * directorycontents = [filemanager contentsofdirectoryatpath: defaultpath error: & error]
- // Matches the string and returns the reverse result. Self = indicates each element in the array.
- Nsstring * match = @ "imagexyz-999.png ";
- Nspredicate * predicate = [nspredicate predicatewithformat: @ "Self = % @", Match];
- Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];
- // Approximate match string, similar to the syntax in SQL
- Nsstring * match = @ "imagexyz *. PNG ";
- Nspredicate * predicate = [nspredicate predicatewithformat: @ "self like % @", Match];
- Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];
- // Case-insensitive matching
- Nsstring * match = @ "imagexyz *. PNG ";
- Nspredicate * predicate = [nspredicate predicatewithformat: @ "self like [cd] % @", Match];
- Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];
- // Regular Expression matching
- Nsstring * match = @ "imagexyz-// d {3} //. PNG ";
- Nspredicate * predicate = [nspredicate predicatewithformat: @ "Self matches % @", Match];
- Nsarray * Results = [directorycontents filteredarrayusingpredicate: predicate];