Objective C plist搜索和随机条目

发布于 2024-11-20 00:41:50 字数 853 浏览 5 评论 0原文

另外几个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一直在等你来 2024-11-27 00:41:50

显然可以使用 -(NSEnumarator *)objectEnumerator; 迭代 NSDictionary 值,您还可以使用 -(NSArray *)allValues 检索所有值; 你也可以看看 -(NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate; 返回一个 NSSet,其中包含已通过测试的值的键(从 Mac OS X 10.6 开始)。

关于第二个问题,我认为没有“更好”的办法。我将这样做:

  1. 使用 -(NSArray *)allKeys; 获取 NSDictionary 的所有键;
  2. 获取随机数
  3. 使用 < 在数组中选取一个键code>-(id)objectAtIndex:(NSUInteger)anIndex;
  4. 使用以下方法检索字典中相应的对象: -(id)objectForKey:(id)aKey;

然后你就得到了你的对象。
希望这有帮助。

编辑:

这是一种迭代 NSDictionary 中的值的简单方法:

// assuming you already have a well initialized dictionary
// first create a container
NSMutableArray *selectedObjects = [[NSMutableArray alloc] init];
// then retrieve an enumerator for the dictionary
NSEnumerator *e = [theDictionary objectEnumerator];
id anObject;
// iterate...
while((anObject = [e nextObject]) != nil) {
    // do what you want with the object
    // in your case each object is an array
    NSArray *theArray = (NSArray *)anObject;
    // ...
    // if you find any of them interesting put it in the first array
    if([[theArray objectAtIndex:0] isEqualToString:@"Jones"]) {
        [selectedObjects addObject:[theArray objectAtIndex:0]];
    }
}
// here are the selected objects in selectedObjects.
// you won't forget to release selectedObjects when you don't need it anymore

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 a NSSet 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 :

  1. Get all the keys of the NSDictionary using -(NSArray *)allKeys;
  2. Get a random number
  3. Pick up a key in the array using -(id)objectAtIndex:(NSUInteger)anIndex;
  4. Retrieve the corresponding object in the dictionary using : -(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 :

// assuming you already have a well initialized dictionary
// first create a container
NSMutableArray *selectedObjects = [[NSMutableArray alloc] init];
// then retrieve an enumerator for the dictionary
NSEnumerator *e = [theDictionary objectEnumerator];
id anObject;
// iterate...
while((anObject = [e nextObject]) != nil) {
    // do what you want with the object
    // in your case each object is an array
    NSArray *theArray = (NSArray *)anObject;
    // ...
    // if you find any of them interesting put it in the first array
    if([[theArray objectAtIndex:0] isEqualToString:@"Jones"]) {
        [selectedObjects addObject:[theArray objectAtIndex:0]];
    }
}
// here are the selected objects in selectedObjects.
// you won't forget to release selectedObjects when you don't need it anymore
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文