【发布时间】: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