tags:

views:

31

answers:

2

hi,

i have a dictionary of array:

    array3 = [[NSDictionary dictionaryWithContentsOfFile:docpath2] retain]; 

no problem here. now i have an NSString *temp which contains some numbers. i would like to check whether this number exist in the dictionary, not each array.

    if([array3 valueForKey:temp])

is this right? doesn't the if loop execute if its true?

<dict>
     <key>123456</key>
     <array>
        <string>low</string>
        <string>High</string>
     </array>
      <key>78910</key>
    <array>
         <string>low</string>
         <string>High</string>
     </array>
  </dict>

for example temp = 78910, i would like it to be found.

thks in adv

A: 

That should work just fine. Why do you think it's not working?

kubi
objectForKey works. :)
Stefan
+1  A: 

Not sure I get your question, but I'll give it a shot.

First, objectForKey: is the method to use as valueForKey: is for Key-Value Coding and will give you strange results should your key start with an @.

Then, if objectForKey: does return a pointer (i.e. the return value is not nil/NULL/0) then the key exists and has a value (which was returned). So yes, you can do:

if ([array3 objectForKey:temp]) {
  // Do something, the dictionary does contain a value for
  // the key referenced by "temp".
}

Now, if your keys a strings like @"123", then you can of course do [array3 objectForKey:@"123"]. But if you got an integer, then you would need to do [array3 objectForKey:[NSString stringWithFormat:@"%d", 123]].

Also note that you can also use NSNumber or NSValue as keys. So you could do:

[myMutableDictionary setObject:someObject forKey:[NSNumber numberWithInt:123]];
myValue = [myMutableDictionary objectForKey:[NSNumber numberWithInt:123]];
if (myValue) {
  // Do something.
}
DarkDust