嵌套 NSArray / NSDictionary 问题
我有一个包含多个 NSDictionaries 的 NSArray 。
我有一个包含 ID 字符串的 NSString
。
我想做的是迭代 NSDictionaries
直到找到与 NSString
匹配的对象。然后我想返回整个 NSDictionary
。
我对 iPhone 开发相当陌生,所以我可能错过了一些明显的东西......提前致谢。 :)
编辑,这是保存的 .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>
<dict>
<key>date</key>
<string>2011/05/20</string>
<key>id</key>
<string>1282007740</string>
<key>name</key>
<string>Test item name</string>
<key>niceDate</key>
<string>May 20, 2011</string>
</dict>
<dict>
<key>date</key>
<string>2010/08/15</string>
<key>id</key>
<string>1282075925</string>
<key>name</key>
<string>Test. Nothing to see here.</string>
<key>niceDate</key>
<string>Aug 15, 2010</string>
</dict>
</array>
</plist>
假设我的 NSString 是“1282075925”。我想获取具有匹配的 id 键的字典。
I have an NSArray
containing several NSDictionaries
.
I have an NSString
containing an ID string.
What I'm trying to do is iterate through the NSDictionaries
until I find the object that matches the NSString
. Then I want to return the entire NSDictionary
.
I'm fairly new to iPhone development, so I'm probably missing something obvious... Thanks in advance. :)
Edit, here's the .plist
file that gets saved out:
<?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>
<dict>
<key>date</key>
<string>2011/05/20</string>
<key>id</key>
<string>1282007740</string>
<key>name</key>
<string>Test item name</string>
<key>niceDate</key>
<string>May 20, 2011</string>
</dict>
<dict>
<key>date</key>
<string>2010/08/15</string>
<key>id</key>
<string>1282075925</string>
<key>name</key>
<string>Test. Nothing to see here.</string>
<key>niceDate</key>
<string>Aug 15, 2010</string>
</dict>
</array>
</plist>
Let's say my NSString is "1282075925". I want to get the dictionary with the id key that matches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此代码应该执行您想要的操作 - 最后,
dict
将指向您正在搜索的NSDictionary
,或者它将是nil
如果没有匹配的字典。This code should do what you want - at the end,
dict
will either point to theNSDictionary
you are searching for, or it will benil
if there was no matching dictionary.如果您正在搜索@“12345”,键为@“id”
If you are searching for @"12345", with a key of @"id"