rich
rich

Reputation: 1227

Objective C plist searching and random entry

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 :)

Upvotes: 0

Views: 591

Answers (1)

user756245
user756245

Reputation:

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

Upvotes: 2

Related Questions