Objective C plist搜索和随机条目
另外几个关于 iPhone 的 plists 和 Objective C 的问题。 这两个问题都与我的 plist 相关,可以在我的最后一个 最后一个问题。
首先是与搜索有关,我知道这是可能的,但是正确的方法是什么?我应该将所有可搜索对象拉入一个数组并使用它来构建一个表吗?或者是否可以迭代 plist 并仅显示匹配项?还是我在这里缺少其他方式?作为下面的一个简单示例,我想带回两个“琼斯”结果:
<dict>
<key>A</key>
<array>
<string>A Jones</string>
<string>A King</string>
</array>
<key>T</key>
<array>
<string>T Jones</string>
<string>T King</string>
</array>
其次,是否可以从 plist 中调用随机结果,我很确定是这样,但同样,正确的结果是什么怎么办?
我承认 plist 有点麻烦,因为在我看来它有点像垃圾形式的 xml。而且我仍然发现迭代 plist 字典在某种程度上非常令人困惑。尽管如此,对这两个问题的任何想法都将不胜感激。
谢谢 :)
another couple of questions about plists and objective c for the iphone.
Both questions relate to my plist which can be seen in my last last question.
First thing is to do with searching, I know this is possible but what is the correct way to go about this? Should I pull all the searchable objects into an array and use this to build a table? Or is it possible to itereate through the plist and simply just show the matches? Or is there some other way I am missing here? As a quick example in the following I would want to bring back the two 'Jones' results:
<dict>
<key>A</key>
<array>
<string>A Jones</string>
<string>A King</string>
</array>
<key>T</key>
<array>
<string>T Jones</string>
<string>T King</string>
</array>
Secondly, is it possible to call up a random result from the plist, I'm pretty sure it is, but again what would be the correct way to go about this?
I will admit to finding the plist a bit of a pain as it seems to me like a bit of a rubbish form of xml. And I am still finding iterating through a plist dictionary pretty confusing to some degree. Still, any thoughts on these two questions would be greatly appreciated.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然可以使用
-(NSEnumarator *)objectEnumerator;
迭代NSDictionary
值,您还可以使用-(NSArray *)allValues 检索所有值;
你也可以看看-(NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate;
返回一个NSSet
,其中包含已通过测试的值的键(从 Mac OS X 10.6 开始)。关于第二个问题,我认为没有“更好”的办法。我将这样做:
-(NSArray *)allKeys;
获取NSDictionary
的所有键;-(id)objectForKey:(id)aKey;
然后你就得到了你的对象。
希望这有帮助。
编辑:
这是一种迭代
NSDictionary
中的值的简单方法:It is obviously possible to iterate through a
NSDictionary
values using-(NSEnumarator *)objectEnumerator;
, you can also retrieve all the values with-(NSArray *)allValues;
and you could also have a look to-(NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate;
which returns aNSSet
containing the keys for the value have passed the test (from Mac OS X 10.6).About the second question, I think there's no 'better' way. Here is how I would do that :
NSDictionary
using-(NSArray *)allKeys;
-(id)objectAtIndex:(NSUInteger)anIndex;
-(id)objectForKey:(id)aKey;
Then you got your object.
Hope this helps.
EDIT:
Here is a simple way to iterate over the values in the
NSDictionary
: