简体   繁体   中英

How to count the number of files starting with a specific string?

I'm new to programming in general so forgive me if this is a stupid question.

I have a series of images in the 'resources' folder of my app. They're named 'thing1_01.png', 'thing1_02.png', 'thing2_01.png', and so on. I have a piece of code which puts these images into a dynamic scrollview based on which 'thing' is currently selected.

Presently my scrollview is set to show a fixed 3 images, which it does very nicely. However, not every 'thing' will have exactly 3 images, so I would like xcode to figure out how many pictures I have for each item.

Is it possible to perform a count on the number of files in my directory which start with (or include at any point) a specific string, for example everything that includes 'thing1'?

NSLog the files in the resources dir that contain thing1 in their filename:

NSError *error = nil;
NSArray *dirContents = [resources contentsOfDirectoryAtPath:@"resources" error:&error];
if ([dirContents count] > 0 && error == nil){
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF contains[dc] %@",@"thing1"];
    NSLog(@"%@", [dirContents filteredArrayUsingPredicate:pred]);
else if ([dirContents count] == 0 && error == nil){
    NSLog(@"Empty directory."); 
} else {
    NSLog(@"error: %@", error); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM