-2

I am using an IF statement to see if the valueForKey is @"T" in my NSDictionary, however i don't think i am accessing the NSDictionary correctly because even when i know the valueForKey is T is never enters the IF statement.

This is what my code looks like:

if ([[getObj valueForKey:@"hasSetID"] isEqualToString:@"T"]) {
        [arrayOfButtonImages addObject:firstButtonImage];
    }

So this if statment is never entered even if it is T.

If any one knows how to test the valueforkey of a NSDictionary it would be greatly appreciated if you could help me with your insight.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • `NSArray *keys = [getObj allKeys];` try this and print it, then you will be able to see all keys of **dictionary**. One more thing may be any white space is append with keys so in this case it would not exact equal to `@"T"`. – TheTiger May 14 '13 at 04:12
  • do you want to search within the Keys of the Dictionary or get its values? – Ahmed Z. May 14 '13 at 04:14
  • 1
    [This works for me, just like it is supposed to work.](http://ideone.com/DIU6LM) –  May 14 '13 at 04:15
  • How do you know the value is `@"T"`? – jscs May 14 '13 at 04:16
  • @H2CO3 - He is doing same ..... I think problem is in how he is making the dictionary. – TheTiger May 14 '13 at 04:19
  • [getObj valueForKey:@"hasSetID"] instead try with [getObj objectForKey:@"hasSetID"] – Manu May 14 '13 at 04:19
  • @VenkatManoharPerepa NO NO NO NO NO, that's what I'm talking about - it's (almost) the same for `NSDictionary`. OP's code **should** work. –  May 14 '13 at 04:19
  • Do you mean objectForKey and valueForKey are same ?? @H2CO3 – Manu May 14 '13 at 04:20
  • @VenkatManoharPerepa It would be enough to read the documentation of `NSDictionary` before giving wrong answers based on lazy assumptions. [Read this link](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000140-BBCIBCDJ), it clearly states what the equivalence and the difference is. –  May 14 '13 at 04:22
  • @TheTiger No, `valueForKey:` is not "better", and it doesn't "work with any class". It has a different use case. –  May 14 '13 at 04:24
  • @H2CO3: Hold on!!!! Do you know the difference between objectForKey and valueForKey ?? – Manu May 14 '13 at 04:24
  • @VenkatManoharPerepa I do, and if you click on the link I provided, you will know it too. (Also, stop duplicating your question marks, one of them is enough.) –  May 14 '13 at 04:25
  • @H2CO3 - Can you explain it ? – TheTiger May 14 '13 at 04:25
  • @JoshCaswell I know because I can NSLog the dictionary and see all the keyValueName = T etc. maybe its not a string? could it be like a bool maybe? or dose every value in a valueforkey of type nsstring? – HurkNburkS May 14 '13 at 04:25
  • 1
    @TheTiger `valueForKey:` is for accessing KVC-compliant properties. `objectForKey:` is the only method that is usable on `NSDictionary` when the key starts with `@`. Neither one is better than the other, they are to be used in different situations. –  May 14 '13 at 04:27
  • @HurkNburkS store `[getObj valueForKey:@"hasSetID"]` in `id` and then you can check its class by using `isKindOfClass:` method. – TheTiger May 14 '13 at 04:29
  • @TheTiger i wnat to check the string in the valueForKey what do you mean by isKindOfClass – HurkNburkS May 14 '13 at 04:30
  • @HurkNburkS - Do you know how to use debugger ? – TheTiger May 14 '13 at 04:31
  • @TheTiger yes, I know how to use it. It shows my NSDictionary and I can look at its keyvalues too. – HurkNburkS May 14 '13 at 04:35
  • for(id key in [getObj allKeys]){ id value = [getObj valueForKeykey]; if([value isKindOfClass:[NSString Class]]) NSLog(@"this is a string"); } – TheTiger May 14 '13 at 04:37
  • do it in this way ...... – TheTiger May 14 '13 at 04:37

2 Answers2

1

This is supposed to work, and it does indeed. The error is somewhere else. Try logging the contents of your dictionary:

NSLog(@"%@", dict);

to see if the key really is @"hasSetID", and the value is indeed @"T". Don't you have an extra whitespace character in one of them? etc.

  • i didnt downvote... when i log the NSDictionary I get this { HasASPID = F; firstInfoSetID = F; secondInfoSetID = F; thirdInfoSetID = T; //etc } – HurkNburkS May 14 '13 at 04:28
  • @HurkNburkS Neither of the keys is `hasSetID` in that. –  May 14 '13 at 04:29
  • Yes both're wrong ..... :) – TheTiger May 14 '13 at 04:30
  • I didnt downvote too. I wont even do that. – Manu May 14 '13 at 04:30
  • I didn't downvote too. – TheTiger May 14 '13 at 04:30
  • @H2CO3 sorry there is a key called hasset its further down its defiantly there i have tested for it by logging it etc. I jsut didnt want to add every valueforkey because there are a few of them.. thats why i added the //etc – HurkNburkS May 14 '13 at 04:32
  • @HurkNburkS Could you please log that key and the corresponding value character-by-character, after having checked that both of them `isKindOfClass:[NSString class]`? –  May 14 '13 at 04:33
  • I think actually they might be of type booL! I just checked where I am making the NSDictionay.. damnit what an idiot.. how do i test for T or F if I am saving that into a BOOl – HurkNburkS May 14 '13 at 04:38
  • 2
    @HurkNburkS If a `BOOL` is stored in an `NSDictionary`, it's generally done by wrapping it in an `NSNumber`. In that case, its descirption is either `0` or `1`, so here you can be sure it's not an `NSNumber` containing a `BOOL`. Log its class using `NSLog(@"%@", [[dict objectForKey:@"hasSetID"] class]);`. –  May 14 '13 at 04:41
0

Maybe you can try this one:

if([[yourDictionary allValues]containsObject:@"T"]){
    // Your code here if T is existing
}
else{
    // your code here if its "T" is not existing
}
Joshua
  • 2,432
  • 1
  • 20
  • 29