在获取文档目录的内容时无法省略@“。DS_Store”文件

时间:2012-12-26 06:12:45

标签: iphone xcode nsdocumentdirectory

我正在处理将音频文件列表存储到我的文档目录中,然后提取它们。

它为我提供了一个音频文件列表以及它给我一个名为@".DS_Store"的文件。在获取内容时,我想留下这个文件目录文件。

除了从数组中删除音频列表或应用@".DS_Store"支票之外,有什么方法可以在获取音频列表时摆脱这种情况。

究竟是什么原因。?

#pragma mark - Saving Audio in Document Directory
-(void)saveAudioinDocumentDirectory:(ASIHTTPRequest *)theRequest
{
    /*save the Audio file in Document Directory */
    NSFileManager *fileManager=[NSFileManager defaultManager];

    NSLog(@"GOT THE SIZe OF AUDIO %d",[[theRequest responseData] length]);
    NSLog(@"AUDIO ID IS %@",[[theRequest userInfo] valueForKey:@"audioIndex"]);

    /*Get the Path  to Application Documents Directory*/
    NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    /*append the neccessary file extension */

    NSString *filepathStr=[NSString stringWithFormat:@"/%@/%@.mp3",docDir,[NSString stringWithFormat:@"%@",[[theRequest userInfo] valueForKey:@"audioIndex"]]];

    /*Check if my crrent file exists in the Documents Directory*/

    if(![fileManager fileExistsAtPath:filepathStr])
    {
        /* file doesnt exists */

        /*create a NSdata of File*/
        NSData *data=[NSData dataWithData:[theRequest responseData]];
        NSLog(@"%@",filepathStr);

        if ([data length] >0 ){


            /*write the File at the Location in Documents Directory */

            [data writeToFile:filepathStr atomically:YES];
            NSLog(@"Successfully saved the file to %@", filepathStr);
        }
        else if([data length] == 0)
        {
            NSLog(@"Nothing was downloaded.");
        }

    }
    /*After saving fetch the path til documents Directory*/
    NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    /*Get the Path for Files */
    NSString *s=[folders objectAtIndex:0];

    /*Fetch the list of Files stored in Documents Directory*/

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];
    NSLog(@"TOTAL NUMBER OF AUDIO FILES %d %@",[contents count],[contents objectAtIndex:0]);

    if([Audiolistforstr isEqualToString:@"AudioListForIntro"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  from document Intro IS %@",Audiolistforstr);

        /*Intro*/
        [AudioListArrForIntro removeAllObjects];
        [AudioListArrForIntro addObjectsFromArray:contents];
        if([AudioListArrForIntro containsObject:@".DS_Store"])
        {
            [AudioListArrForIntro removeObject:@".DS_Store"];

        }
        NSLog(@"FINAL LIST %@",AudioListArrForIntro);

    }
    else if([Audiolistforstr isEqualToString:@"AudioListForCredits"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  from document credit IS %@",Audiolistforstr);

        /*credits*/
        [AudioListArrForCredits removeAllObjects];
        [AudioListArrForCredits addObjectsFromArray:contents];
        if([AudioListArrForCredits containsObject:@".DS_Store"])
        {
            [AudioListArrForCredits removeObject:@".DS_Store"];

        }
    NSLog(@"FINAL LIST %@",AudioListArrForCredits);

    }


    /* Did we find anything? */
    if([Audiolistforstr isEqualToString:@"AudioListForIntro"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  fromRELOADNG TABLE Intro IS %@",Audiolistforstr);

        /*Intro*/
        if ([AudioListArrForIntro count] == 0)
        {

        }
        else
        {
            UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioIntroTag];
            [(UITableView *)[vw viewWithTag:tblIntroAudioListTag] reloadData];
        }
    }
    else if([Audiolistforstr isEqualToString:@"AudioListForCredits"])
    {
        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  fromRELOADNG TABLE Intro IS %@",Audiolistforstr);

        /*Credits*/
        if ([AudioListArrForCredits count] == 0)
        {
        }

        else
        {

            /*AudioListForCredits*/

            UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioCreditsTag];
            [(UITableView *)[vw viewWithTag:tblCreditsAudioListTag] reloadData];
        }

    }

}

任何帮助都将不胜感激。

由于 维卡斯

4 个答案:

答案 0 :(得分:6)

您可以在.DS_Store行........即

之后检查NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray * dirContents = [[NSMutableArray alloc] initWithArray:docDir];
if([docDir containsObject:@".DS_Store"])
{
    [dirContents removeObject:@".DS_Store"];
}

由此,dirContents删除了.DS_Store

的条目

答案 1 :(得分:2)

过滤文档目录内容。例如,如果您的扩展名为.mp3的音频文件,那么您可以获得所有mp3文件,如下所示:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];

directoryContent = [directoryContent filteredArrayUsingPredicate:
                                            [NSPredicate predicateWithFormat:@"pathExtension ==[c] %@", @"mp3"]];

这将省略除mp3文件之外的所有其他文件。 一切顺利!!!

答案 2 :(得分:1)

您正在使用的API:

NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];

返回在路径中找到的所有文件,其中包括the ".DS_Store" file

我建议将“contents”分配给可变数组,例如:

NSMutableArray * contents = 
    [[NSMutableArray alloc] initWithArray: [fileManager contentsOfDirectoryAtPath:s error:NULL]];` 

并遍历数组以查找并删除任何和所有没有“.mp3”作为路径扩展名的文件。

我还建议不要使用大写字母来启动任何变量(例如,而不是“Audiolistforstr”,使用“audiolistforstr”或更好,“arrayofAudioFiles”)。 Objective C最佳实践是用小写字母开始所有变量和方法。

答案 3 :(得分:1)

运作良好..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];

for (int i = 0; i < [imageFilenames count]; i++)
    {

    NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];

        if (![[imageFilenames objectAtIndex:i]isEqualToString:@".DS_Store"])
        {
          UIImage *myimage = [UIImage imageWithContentsOfFile:imageName];
          UIImageView *imageView = [[UIImageView alloc] initWithImage:_myimage];
        }
    }