tags:

views:

34

answers:

4

Hi, this is my code

NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    NSArray *Children = [dictionary objectForKey:@"Children"];
    NSArray *Title = [dictionary objectForKey:@"Title"];

if([Children count] == 0) {
    if(Title =="A #1"){
UIImageView *tmp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg2.png"]]; 
        [dvController.detailImage addSubview:tmp]; 
        [tmp release]; 

} i am not able to compare "Title", at if(Title == "A #1") , its giving warning "Compariosn of distinct pointer types lacks a cast" how can i compare Title in if condition. i also tried

if([Title isequalToString:@"A #1"])...but its not working for me.

Sry for unformated question. regards

+1  A: 
NSArray *Title = [dictionary objectForKey:@"Title"];

Title is type of NSArray and "A #1" is NSString Compiler is 100% correct as always... you can't compare this two object because they are of distinct type

you might want something like

if([[Title objectAtIndex:0] isEqualToString:@"A #1"]])

OR

NSString  *Title = [dictionary objectForKey:@"Title"];
if([Title isEqualToString:@"A #1"]])
mihirpmehta
It's not even NSString... it misses a @.
Daniel Bleisteiner
Thank for reply...but i tried ur snippet..app gets crashed...saying" *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6f22760'"
shishir.bobby
Yes exactly... but i thought pointing out that mistake would confuse him...
mihirpmehta
then that title is not NSArray at all... Or it doesn't contain NSString...i can not tell what is your problem unless i know what you're storing for Title key in your dictionary..
mihirpmehta
i have put another option in my answer you can try that also...
mihirpmehta
this worked"NSString title = [dict objectForKey:@"Title"];if ([title isEqualToString:@"my string"]) { /* do it */ }"
shishir.bobby
+2  A: 

You declared Title as an NSArray*, so it's obviously a different type from a string and can't be compared to a string. (Perhaps this is a typo?)

Perhaps something more like:

NSString myTitleString = (NSString *)[dictionary stringForKey"@"Title"]

if ( [ myTitleString isEqualToString:@"foo") ] ) { ... }

is what you want?

hotpaw2
Thanksi tried ur code also, but on declaring it to NSstring, warning comes, up with, "NSdictonary may not respond -stringFor Key"...wat do u suggest now?
shishir.bobby
Oops. You need to get the NSArray from the NSDictionary, and the NSString from the NSArray. My code skips a step there. Can you figure that out and insert the missing step? Or do you need code?
hotpaw2
+1  A: 

Hello,

If I understand the code, which is a bit hard because of the formatting. You are defining Title as NSArray. If you want to make a string comparison you need an NSString. So you are either declaring "Title" of the wrong type, or you are expecting another array from the dictionary where you have your strings. I would assume it's the first. This should work:

NSString *titleString = [dictionary objectForKey"@"Title"];

if([titleString isEqualToString:@"A #1"]){
}
Merrimack
+2  A: 

Your data structure looks weird... what do you want to store in that dictionary?

But as others already said: if Title is of NSArray you won't get true when compared to a string or NSString.

If Title should be of NSString you might call this:

NSString *title = [dict objectForKey:@"Title"];
if ([title isEqualToString:@"my string"]) { /* do it */ }
Daniel Bleisteiner
That worked perfectly..thanks
shishir.bobby