Ios 提取扩展名并将图像名称保存在数组中

Ios 提取扩展名并将图像名称保存在数组中,ios,arrays,nsarray,Ios,Arrays,Nsarray,我在“ProjectMages”文件夹中有一些图像,我想从上面的文件夹中检索图像文件名并将其存储在数组中,然后我想提取这些图像文件的扩展名并将其存储在另一个数组中。我能够从文件夹中检索图像文件名并提取扩展名,但我不知道如何将提取的文件名存储在另一个数组中。下面是代码 //get image name from the folder NSError *error; NSFileManager *fileMgr = [NSFileManager defaultManager];

我在“ProjectMages”文件夹中有一些图像,我想从上面的文件夹中检索图像文件名并将其存储在数组中,然后我想提取这些图像文件的扩展名并将其存储在另一个数组中。我能够从文件夹中检索图像文件名并提取扩展名,但我不知道如何将提取的文件名存储在另一个数组中。下面是代码

//get image name from the folder
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *directory = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"ProjectImages"];

    NSArray *files=[fileMgr contentsOfDirectoryAtPath:directory error:&error];

//extract the extension
NSString *imageNames=nil;
    for (NSString *name in files) {
        if (!imageNames) imageNames = [[name lastPathComponent] stringByDeletingPathExtension];
        else imageNames = [imageNames stringByAppendingFormat:@" %@",[[name lastPathComponent] stringByDeletingPathExtension]];

    }
NSLog(@"%@",imageNames); --> This returns one single name with all the image name, which i don't want, instead i want to store each extracted file name in array and wanted to compare the string entered by the user with this array. how to do it ?

您没有在任何数组中存储任何内容。您正在构建一个以空格分隔的文件名字符串。谢谢您shashi3456643!我使用if([array containsObject:user_entered_string])来比较用户输入的字符串是否在可变数组中,但它没有;我不会给出任何结果。以下是(文件中的NSString*名称){NSString*imageName=[[name lastPathComponent]stringByDeletingPathExtension];[imgaftext addObject:imageName];if([imgaftext containsObject:message]){//message是用户NSLog(@“是”);}输入的字符串的代码你是说如果已经存在就不要添加重复项,那么不要添加你可以使用inRange.if([mutableArray indexOfObject:object inRange:NSMakerRange(0,[mutableArray count])!=NSNotFound){//duplicate}谢谢你的回复shashi…上面发布的代码工作了…“message”返回了不同的字符串,所以它无法比较..我不得不修剪几个字符..修剪后,它工作了..再次感谢
You are not storing image name in array
 Try this way


    //get image name from the folder
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *directory = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"ProjectImages"];
    NSArray *files=[fileMgr contentsOfDirectoryAtPath:directory error:&error];
    //extract the extension
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (NSString *name in files) {
        NSString imageName = [[name lastPathComponent] stringByDeletingPathExtension];
if ([array indexOfObject:imageName inRange:NSMakeRang(0,[array count])]!=NSNotFound){ 
//duplicate
 }
else
 {
 [array addObject:imageName];
 }

}
    NSLog(@"%@",array);