【问题标题】:Get Files Inside a folder in Document directory in iPhone在 iPhone 的 Document 目录中的文件夹中获取文件
【发布时间】:2012-07-23 16:24:25
【问题描述】:

我是 iPhone 新手,

我制作了一个应用程序,我正在下载一本书并将其存储在一个名为book1Folder 的文件夹中,该文件夹位于Document directory 内。

现在我想要数组中所有书籍的名称,book1Folder 中有 2 本书,但是当我编写此代码时,它显示数组计数为 3。

这是我的代码 sn-p,

-(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 为何而来?

我们将不胜感激。

【问题讨论】:

  • .DS_Store 文件由 os 创建。你可以忽略它。
  • 为什么即使我写了if([[files pathExtension] isEqualToString:@"epub"]),它也会被添加到我的数组中,然后只将书添加到我的数组中。

标签: iphone ipad nsmutablearray nsfilemanager nsdocumentdirectory


【解决方案1】:

.DS_Store 是 mac 中的隐藏文件,用于将文件消息存储在文件夹中。您可以将其删除

【讨论】:

    【解决方案2】:

    将 if 块放在大括号中。它只影响 if 之后的第一行。

    如下所示;

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

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2016-07-20
        • 2010-12-26
        • 1970-01-01
        • 2023-03-28
        • 2012-07-31
        • 2014-08-13
        • 2010-12-02
        • 2016-09-04
        • 2015-12-26
        相关资源
        最近更新 更多