Comparison of distinct pointer types lacks a cast
this is my code
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
NSArray *Children = [dictionary objectForKey:@"Children"];
NSA开发者_StackOverflow中文版rray *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
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 */ }
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?
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"]){
}
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"]])
精彩评论