Reputation: 3233
I am getting the following error
-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x3c168090
on this line of code
cell.offerTitle.text = [voucherData objectForKey:@"offer_title"];
Could someone help me correct the problem please?
Thanks Oliver
Upvotes: 1
Views: 1662
Reputation: 2150
i make some changing for noa's answer
-(NSDictionary*)safeData:(NSDictionary*)dict{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:dict];
NSArray *keys = dictionary.allKeys;
for (int i = 0; i < keys.count; i++){
id value = [dictionary objectForKey:[keys objectAtIndex:i]];
// you can add recursive here
value = [value isKindOfClass:[NSNull class]] ? @"" : value;
[dictionary setObject:value forKey:[keys objectAtIndex:i]];
}
return dictionary; }
and use
dictionary = [self safeData:dictionary];
Upvotes: 0
Reputation: 535229
One candidate for best practice here is to use isEqual:
, not isEqualToString:
. That way, if what you get is not a string, you won't get an error and the equality test will be failed in good order.
On the other hand you could argue that isEqualToString:
was a good choice, because when what you got was not a string, you got an error that alerted you to the issue!
EDIT: But that's wrong; see the comments below. The isEqualToString:
message is coming from UIKit, not from the OP's own code.
Upvotes: 0
Reputation: 17208
Is voucherData
an NSDictionary?
It's possible there's an NSNull
in your dictionary, and when the dictionary is trying to find the object for offer_title
, it's running into trouble.
Another possibility is that [voucherData objectForKey:@"offer_title"]
is returning [NSNull null]
, and the label is barfing when you try to pass that instead of a string.
Try setting a breakpoint in objc_exception_throw
and read the stack trace – that will give you a much better idea of what's going on.
Added:
id value = [voucherData objectForKey:@"offer_title"];
if ([value isKindOfClass:[NSNull class]])
cell.offerTitle.text = @"";
else
call.offerTitle.text = value;
or
id value = [voucherData objectForKey:@"offer_title"];
cell.offerTitle.text = [value isKindOfClass:[NSNull class]] ? @"" : value;
Upvotes: 6
Reputation: 89509
What that line from the console is likely telling you is that "voucherData
" is not the "NSDictionary
" object that you assume that it is.
Also make sure that "offerTitle
" in your cell is a valid UITextField
as well.
Upvotes: -1