【问题标题】:Get Contents of Files In Array on iOS在 iOS 上获取数组中文件的内容
【发布时间】:2021-11-01 22:31:07
【问题描述】:

在我的 iOS 应用程序中,我需要查看已存储多个已创建 PLIST 的文档目录之一。每个 PLIST 只包含一个名称数组。我需要为每个 PLIST 获取文件名以及文件中的名称数组。我坚持这样做的最佳方式。当我运行各种日志时,我得到了正确的路径,但数组总是返回 Null。

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     
     NSString *devotionals = [documentsDirectory stringByAppendingPathComponent:@"devotionals"];
     self.files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:devotionals error:nil];
     self.Array = [NSMutableArray arrayWithCapacity:[self.files count]];

     for (id myArrayElement in self.files) {
         NSLog(@"names%@", myArrayElement);
         
         self.theRealDeal = [devotionals stringByAppendingPathComponent:myArrayElement];
         [self.Array addObject:self.theRealDeal];
         NSLog(@"REALDEAL %@", self.theRealDeal);
         NSLog(@"ARRAY %@", self.Array);
         
         

        // NSLog(@"Loop %@", self.Array);
     }

PLIST 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>A Common Love</string>
    <string>A Shield About Me</string>
    <string>A Wonderful Savior</string>
    <string>Above All</string>
    <string>Above All Else</string>
    <string>Ah, Lord God</string>
    <string>Alas and Did</string>
    <string>All Because of God's Amazing Grace</string>
</array>
</plist>

更新:

我在我的 OP 中稍微更改了代码。根据日志,self.theRealDeal 将为我提供目录中每个PLIST 的路径。我尝试将这些路径添加到 NSMutableArray 但 NSLog 只显示 (

【问题讨论】:

  • self.Array 还是 self.array ?
  • 你能提供一个plist文件的例子吗?这可能只是一个错误的内容。
  • @PtitXav 我添加了一个我在应用程序中创建的 PLIST 文件的示例。

标签: ios xcode for-loop nsarray nsmutablearray


【解决方案1】:

这是我如何让它工作的。

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject: @"LoggedIn" forKey:@"isLogged"];
        [defaults synchronize];
        
        
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *devotionals = [documentsDirectory stringByAppendingPathComponent:@"devotionals"];

        self.files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:devotionals error:nil];
       // NSLog(@"FILES %lu", (unsigned long)[self.files count]);
        self.Array = [[NSMutableArray alloc] initWithCapacity:[self.files count]];
        //self.Array = [NSMutableArray arrayWithCapacity:[self.files count]];

        for (id myArrayElement in self.files) {
            
            self.theRealDeal = [devotionals stringByAppendingPathComponent:myArrayElement];
            [self.Array addObject:self.theRealDeal];
        
        }
        for (id finishHim in self.Array) {
            self.Array = [[NSMutableArray alloc] initWithContentsOfFile:finishHim];
            NSString *theFileName = [[finishHim lastPathComponent] stringByDeletingPathExtension];
           
            
            
        }

【讨论】:

  • 希望这只是一个错字,但您在第二个循环中重新初始化了 self.Array(即在 self.Array 上循环时)
猜你喜欢
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 2015-10-01
相关资源
最近更新 更多