NSDictionary PList 中的 iPhone 对象
在我的应用程序中,我有一个部分从保存的 plist 加载,该 plist 有 2 个嵌套字典,如下所示:
<?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>
<dict>
<key>color</key>
<string>yellow</string>
<key>lang</key>
<string>US</string>
<key>name</key>
<string>Peter</string>
<key>uid</key>
<string>1</string>
</dict>
<key>2</key>
<dict>
<key>color</key>
<string>blue</string>
<key>lang</key>
<string>US</string>
<key>name</key>
<string>Josh</string>
<key>uid</key>
<string>2</string>
</dict>
<key>3</key>
<dict>
<key>color</key>
<string>red</string>
<key>lang</key>
<string>DE</string>
<key>name</key>
<string>Susan</string>
<key>uid</key>
<string>3</string>
</dict>
</dict>
</plist>
现在我想从键 2 访问类似外部字典的字符串,内部键颜色的值(蓝色) 我尝试制作 2 个循环,它适用于外部字典,但我无法访问内部字典
NSMutableDictionary *savedData = [NSMutableDictionary dictionaryWithContentsOfFile:path]; // This contains all data from plist
for (int x=0; x<[savedData count]; x++) {
NSString *itemNumber = [NSString stringWithFormat:@"%d", x+1];
//This prints out the correct inner dictionary
NSLog(@"item#%@: %@",itemNumber,[savedData objectForKey:itemNumber]);
for (NSDictionary *dict in [savedData objectForKey:itemNumber]) {
//prints out color, lang, uid, but no key-value pairs
NSLog(@"dict: %@",dict);
}
}
我想知道如何直接访问内部字典内的键值对,任何人都可以给我一个正确的方向,请?
in my app I have a section where I load from a saved plist which has 2 nested dictionaries like this:
<?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>
<dict>
<key>color</key>
<string>yellow</string>
<key>lang</key>
<string>US</string>
<key>name</key>
<string>Peter</string>
<key>uid</key>
<string>1</string>
</dict>
<key>2</key>
<dict>
<key>color</key>
<string>blue</string>
<key>lang</key>
<string>US</string>
<key>name</key>
<string>Josh</string>
<key>uid</key>
<string>2</string>
</dict>
<key>3</key>
<dict>
<key>color</key>
<string>red</string>
<key>lang</key>
<string>DE</string>
<key>name</key>
<string>Susan</string>
<key>uid</key>
<string>3</string>
</dict>
</dict>
</plist>
Now I want to access string like outer dictionary from key 2, value for inner key color (blue)
I tried make 2 loops, and it works for the outer dictionary but I can't access the inner
NSMutableDictionary *savedData = [NSMutableDictionary dictionaryWithContentsOfFile:path]; // This contains all data from plist
for (int x=0; x<[savedData count]; x++) {
NSString *itemNumber = [NSString stringWithFormat:@"%d", x+1];
//This prints out the correct inner dictionary
NSLog(@"item#%@: %@",itemNumber,[savedData objectForKey:itemNumber]);
for (NSDictionary *dict in [savedData objectForKey:itemNumber]) {
//prints out color, lang, uid, but no key-value pairs
NSLog(@"dict: %@",dict);
}
}
I'd like to know how to directly access key value pairs inside the inner dictionary, could anyone give me a kick in the right direction, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
技巧是嵌套方法调用,第一个 [savedData objectForKey:2] 返回您可以再次调用该方法的内部字典对象。希望这能按预期工作。
最好的,
罗宾
How about this:
The trick is to nest the method calls, the first one [savedData objectForKey:2] returns you the inner dictionary object on which you can call the method again. Hope this works as intended.
Best,
Robin
NSLog(@"%@", [savedData valueForKeyPath:@"1.color"]);
黄色
NSLog(@"%@", [savedData valueForKeyPath:@"1.color"]);
yellow
通常,当您的键是字符串时,您将使用
valueForKey:
:Typically, you would use
valueForKey:
when your keys are strings: