iphone - XCode:从plist读取数据到NSmutablearray

标签 iphone ios xcode nsmutablearray plist

这是我的问题。我有一个名为 myPlist 的 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">    
<dict>    
<key>1</key>    
<array>    
<string>Dog</string>    
<string> empty </string>    
<true/>    
</array>    
<key>2</key>    
<array>    
<string>Car</string>    
<string> empty </string>    
<false/>    
</array>    
<key>3</key>    
<array>    
<string>Table</string>    
<string> empty </string>    
<true/>    
</array>
</dict>    

每个键都有三个值。两个是字符串,一个是 bool 值。我尝试了“通常的方法”来用 plist 填充 nsmutablearray,这样我就可以在 TableView 中显示它,但 NSMutableArray 始终为空。我首先尝试了这个:

NSMutableArray *mutable = [[NSArray arrayWithContentsOfFile:
             [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"]] retain];

然后我也尝试了这个,但它仍然返回 0 个对象。

 NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];

        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

        NSArray *mutable = [NSArray arrayWithArray:[dict objectForKey:@"Root"]];

我总是使用类似字典的plist,这是我第一个使用这种结构的plist,所以我认为我做错了什么。顺便说一句,我有没有提到我是菜鸟? :)

请帮忙。

编辑 感谢 Rob,我现在可以使用这一行读取数据:

NSDictionary *dogObject = [dict objectForKey:@"1"];

但是我如何将数据传输到 NSMutableArray 以便稍后编辑数据?或者我可以使用字典保存编辑后的数据吗?

最佳答案

您缺少顶层的结束标记 plist标签。我认为这是一个复制/粘贴错误。

如果添加缺失的</plist>最后,这是一个有效的属性列表。它包含一个顶级字典,因此您必须将其加载到 NSDictionary 中。 ,不是NSArray (或NSMutableArray)。

顶级字典不包含键 Root .它包含三个键:1 , 2 , 和 3 .所以你可以像这样从中获取一个对象:

NSDictionary *dogObject = [dict objectForKey:@"1"];

关于iphone - XCode:从plist读取数据到NSmutablearray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447136/

相关文章:

Xcode 9 Beta - 无法添加新的 Playground 页面

iphone - 在一个 View 中隐藏状态栏会在所有 View 中隐藏它

iphone - iOS UITableView索引是透明的

ios - 领英 API : The token used in the OAuth request has been revoked

ios - UINavigationController 中的 UITableViewController 来表示未知深度的树结构

iOS/Xcode - 从 Dropbox 选取文件并传输到公共(public)服务器

swift - 从自定义 @IBDesignable UITextField 类控制占位符文本颜色。 swift 4.2、Xcode 10

iphone - iOS iPad 启动横向,推送 VC,popVC, View 定位错误

iphone - NSString stringWithFormat 很慢

iphone - ios 推特 OATH 集成不起作用