我目前正在阅读应用文档目录中的文件列表。
I am currently reading the list of files present in my app document directory.
NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSError* error = nil; NSMutableArray *myArray = (NSMutableArray*)[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectory error:&error]retain];但是在显示我要显示的文件列表之前,所有文件都应该正确排序。
But before showing the list of files i want to show the all files should be sorted properly.
我该怎么做?
推荐答案你的myArray将是一个NSStrings数组。由于它们都在同一个目录中,你可以这样排序:
Your myArray will be an array of NSStrings. Since they are all in the same directory you can sort it like this:
NSArray * sortedArray = [myArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];现在重要的是要注意 contentsOfDirectoryAtPath:documentDirectory错误:& error 返回目录和文件。所以在获取排序数组之前,如果你只想要文件,
Now it is important to note that contentsOfDirectoryAtPath:documentDirectory error:&error returns both directories and files. So before getting the sorted array, if you want just files,
NSMutableArray *tempArray=[[NSMutableArray alloc] init]; for(NSString *str in myArray) { if([[NSFileManager defaultManager] fileExistsAtPath:str] [tempArray addObject:str]; }现在对这个tempArray而不是myArray进行排序,你就完成了
Now sort this tempArray instead of myArray and you're done
更多推荐
从文档目录中读取文件时的排序
发布评论