[英]Get Files Inside a folder in Document directory in iPhone
我是iPhone新手,
我制作了一个应用程序,我正在下载一本书并将其存储在一个名为book1Folder
的文件夹中,该文件夹位于Document directory
。
现在我想要我的数组中所有书籍的名称, book1Folder
有2本书,但是当我写这段代码时,它显示我的数组是3。
这是我的代码片段,
-(void)viewWillAppear:(BOOL)animated{
NSString *files;
NSString *Dir=[self applicationDocumentsDirectory];
Dir=[Dir stringByAppendingPathComponent:@"book1Folder"];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:Dir];
Downloadedepubs = [[NSMutableArray alloc]init];
while(files = [direnum nextObject])
{
if([[files pathExtension] isEqualToString:@"epub"])
NSLog(@"files=%@",files);
[Downloadedepubs addObject:files];
}
}
我的日志只显示2本书的名称,但是当我遍历数组时,它包含3个对象。
[Downloadedepubs ObjectAtIndex:0]=.DS_Store;
[Downloadedepubs ObjectAtIndex:1]=abcd;
[Downloadedepubs ObjectAtIndex:2]=pqrs;
那是什么.DS_Store
为什么要来?
任何帮助将不胜感激。
在大括号中获取if块。 它只影响if之后的第一行。
如下;
if([[files pathExtension] isEqualToString:@"epub"]) {
NSLog(@"files=%@",files);
[Downloadedepubs addObject:files];
}
.DS_Store是mac中的一个hiden文件,用于将文件消息存储在Folder中。您可以将其删除
尝试这个:
NSDirectoryEnumerator* e = [[NSFileManager defaultManager] enumeratorAtPath:yourPathhere];
// Ignore any files except XYZ.epub
for (NSString* file in e)
{
if (NSOrderedSame == [[file pathExtension] caseInsensitiveCompare:@"epub"])
{
// Do something with file.epub
[Downloadedepubs addObject:files];
}
else if ([[[e fileAttributes] fileType] isEqualToString:NSFileTypeDirectory])
{
// Ignore any subdirectories
[e skipDescendents];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.