views:

76

answers:

2

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"&gt;
<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.

+2  A: 

If you are searching for @"12345", with a key of @"id"

for (NSDictionary* dictionary in array1) {
   id value = [dictionary valueForKey:@"id"];
   if ([value isKindOfClass:[NSString class]]) {
      NSString* string = (NSString*) value;
      if ([string isEqualToString:@"12345"]) {
          return dictionary;
      }
   }
}
return nil;
Akusete
+2  A: 

This code should do what you want - at the end, dict will either point to the NSDictionary you are searching for, or it will be nil if there was no matching dictionary.

// (These are assumed to exist)
NSArray *dictionaries;
NSString *idToFind; 



NSDictionary *dict = nil;
for (NSDictionary *aDict in dictionaries) {
    if ([[aDict objectForKey:@"id"] isEqualToString:idToFind]) {
        dict = aDict;
        break;
    }
}
Nick Forge
+1 for style. But will it throw a 'selector not supported' error if the value for an 'id' field is an array or other type besides NSString?
Akusete
That's a good point. If the plist is created and maintained by you, it's debatable whether it's worth going to the effort of defensive coding for these sorts of issues. When it comes to static plist data, I usually don't put those sorts of defensive checks in there, since I prefer concise (or more to the point, readable) code. If I'm working with other coders, or the plist might be edited by someone else, I usually will. It's a judgement call.
Nick Forge
I'm not worried about it being a type other than NSString. I tried your solution, as it's especially simple and elegant. Unfortunately, it won't match the string to the dictionary for some reason. I put in some NSLogs to debug it, but I can't seem to figure out where it's going wrong. The values I'm plugging in should match, and the routine that created the plist is inserting an NSString, so it's not a type issue...
redwall_hp
[aDict objectForKey:@"id"] *should* be equal to idToFind, but the condition isn't validating as true. The NSLogs are outputting the same values, but 1282075925 just doesn't seem to equal 1282075925 for some reason. I'm loading the plist with myArray = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
redwall_hp
You should be able to make sure that the object is in fact an `NSString` using the debugger (an `NSNumber` may appear to be the same if you use `NSLog()`). Apart from that, separate the `if` statement into two statements (`NSString *idString = [aDict objectForKey:@"id"];` and `if ([idString isEqualToString:idToFind]) {`). You can then use `NSLog` or the debugger to investigate exactly what's not working. Also, just in case you didn't know, `id` is an Obj-C keyword - make sure you don't use it for a variable name.
Nick Forge
I'm not using "id" as a variable. :) I switched the aDict to a different line, and it runs the same. I added a couple of breakpoints and whatis'd idString and my editId (equivalent to your idToFind) variable. Both were listed as NSStrings. NSLog shows both of the values as the same...
redwall_hp
Here's what I have, in case I'm missing something glaringly obvious: http://pastie.org/1101491
redwall_hp
redwall_hp
It's working now. Thanks for the help, and the opportunity to think out loud. :)
redwall_hp