Get Files Inside a folder in Document directory in iPhone

邮差的信 提交于 2019-12-11 08:48:07

问题


I am new to iPhone,

I made an app in which, i am downloading a book and storing it in a folder called book1Folder which is inside Document directory.

now i wants name of all books in my array, there are 2 books inside a book1Folder but when i write this code it shows me array count is 3.

Here is my code snippet,

-(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];
    }
}

My log shows only name of 2 book, but when i loop through array it contains 3 objects.

[Downloadedepubs ObjectAtIndex:0]=.DS_Store;
[Downloadedepubs ObjectAtIndex:1]=abcd;
[Downloadedepubs ObjectAtIndex:2]=pqrs;

What is that .DS_Store why it is coming ?

Any help will be appreciated.


回答1:


Get your if block in curly brackets. It affects only first line after if.

Like below;

if([[files pathExtension] isEqualToString:@"epub"]) {
            NSLog(@"files=%@",files);
            [Downloadedepubs addObject:files];
}



回答2:


.DS_Store is a hiden file in mac to store your files message in the Folder.you can delete it




回答3:


Try this:

  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];
    }
}


来源:https://stackoverflow.com/questions/11646325/get-files-inside-a-folder-in-document-directory-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!