gpt4 book ai didi

iphone - Plist Array 到 NSArray 没有对象

转载 作者:行者123 更新时间:2023-12-01 17:21:39 25 4
gpt4 key购买 nike

所以我之前已经搜索、阅读并成功完成了这项工作,但我遇到了这个问题。

我有一个 plist,它是一个字符串数组。我没有将任何对象读入数组。我成功地为另一个 Controller 中的字典做到了这一点。所以这是我的代码。

    // Open plist and get brands
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"brands" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:plistPath]) {
NSLog(@"The file exists");
} else {
NSLog(@"The file does not exist");
}

_brands = [NSArray arrayWithContentsOfFile:plistPath];

_catList = [[[NSMutableArray alloc] initWithCapacity:[_brands count]] autorelease];

这是我的 list 。请注意,_brands 数组定义为 NSArray *brands,属性设置为非原子、只读,并合成为brands = _brands。
<?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">
<dict>
<key>Root</key>
<array>
<string>Brand A</string>
<string>Brand B</string>
<string>Brand C</string>
<string>Brand D</string>
</array>
</dict>
</plist>

我看到有一个标签,但在 Xcode 中它显示为一个带有字符串的数组。不是字典>数组>许多字符串

最佳答案

PLists 本质上是字典(注意 标签)。将文件读入 NSDictionary,然后使用 objectforKey: 向字典询问数组方法和 Root key 。

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
_brands = [dict objectForKey:@"Root"];

关于iphone - Plist Array 到 NSArray 没有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8044213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com