从文档目录中读取文件时的排序

编程入门 行业动态 更新时间:2024-10-11 07:28:58
本文介绍了从文档目录中读取文件时的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在阅读应用文档目录中的文件列表。

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

更多推荐

从文档目录中读取文件时的排序

本文发布于:2023-11-28 16:25:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   文件   目录中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!