我想将文档目录中的文件列表添加到字符串数组中。不确定如何做到这一点,这是我到目前为止。我想加载/存储只包含单词' bottom'的文件。在数组中的文件名中。我该怎么做?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileMan = [[NSFileManager alloc]init];
NSArray *files = [fileMan contentsOfDirectoryAtPath:documentsDirectory error:nil];
for(int i =0; i<files.count; i++){
}
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString rangeOfString:@"bottom"]];
//THIS IS HARDCODED ARRAY OF FILE-STRING NAMES
NSArray *bottomArray =[NSArray arrayWithObjects: @"bottom6D08B918-326D-41E1-8A47-B92F80EF07E5-1240-000005EB14009605.png", @"bottom837C95CF-85B2-456D-8197-326A637F3A5B-6021-0000340042C31C23.png", nil];
答案 0 :(得分:2)
您需要检查files
数组中的每个文件:
NSFileManager *fileMan = [NSFileManager defaultFileManager];
NSArray *files = [fileMan contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *bottomArray = [NSMutableArray array];
for (NSString *file in files) {
if ([file rangeOfString:@"bottom"].location != NSNotFound) {
[bottomArray addObject:file];
}
}
答案 1 :(得分:1)
除了@ rmaddy的方法之外,另一个选择是使用NSPredicate
的实例来过滤文件名数组:
NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains 'bottom'"];
NSArray *matchedNames = [filenames filteredArrayUsingPredicate:predicate];