【问题标题】:iOS: Load dictionaries from plist file to an arrayiOS:将字典从 plist 文件加载到数组
【发布时间】:2012-07-19 13:34:44
【问题描述】:

我正在尝试将以下 plist 文件加载到 NSMutableArray

<?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>Item 0</key>
<dict>
    <key>title</key>
    <string>East Coast</string>
</dict>
<key>Item 1</key>
<dict>
    <key>title</key>
    <string>West Coast</string>
</dict>
<key>Item 2</key>
<dict>
    <key>title</key>
    <string>South Rhodes</string>
</dict>
</dict>
</plist>

我正在使用以下 Objective C 代码

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Load data from the plist file
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
    self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}

plist 的名称是 Menu.plist。

代码执行后menuArray为空。

奇怪的是,Apple 的“QuickContacts”示例代码中使用了相同的代码,并且一切正常。

感谢您的宝贵时间。

【问题讨论】:

    标签: ios dictionary load plist


    【解决方案1】:

    你的 plist 文件定义了一个字典;事实上,它的根对象是一个dict

    <plist version="1.0">
    <dict>
    

    所以要将其加载到某些数据结构中,您不妨尝试使用 NSDictionary:

    self.menuDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    

    或者,您可以更改您的 plist 以便它定义一个字典。如果你使用 Xcode plist 编辑器,这并不容易做到,否则你面前有一点文本编辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多