I did search on how to check if NSDictionary key exists or not and came up with the solution. But still it throws me an error saying adding null value to the key. I am not sure if my code is correct or not. If anyone has any idea about this can help me.
NSDictionary *result;
id myImageURL = [result objectForKey:@"url"];
if ((NSNull *)myImageURL == [NSNull null])
myImageURL = @"";
id myImage = [result objectForKey:@"image"];
if ((NSNull *)myImage == [NSNull null])
myImage = @"";
Check if null add nothing and if not add the value. But it still gives me an error dont know why.
/****OUTPUT*****/
2011-08-11 14:56:06.668 Tab_Table_Win[6510:207] RESULTS : {
image = "<UIImage: 0xbc332c0>";
url = "http://a3.twimg.com/profile_images/999228511/normal.jpg";
}
2011-08-11 14:56:06.669 Tab_Table_Win[6510:207] url : http://a3.twimg.com/profile_images/999228511/normal.jpg
2011-08-11 14:56:06.670 Tab_Table_Win[6510:207] IMage : <UIImage: 0xbc332c0>
/*****Breaks Here ***/
2011-08-11 14:56:06.876 Tab_Table_Win[6510:207] RESULTS : {
}
2011-08-11 14:56:06.878 Tab_Table_Win[6510:207] url : (null)
2011-08-11 14:56:06.879 Tab_Table_Win[6510:207] IMage : (null)
2011-08-11 14:56:06.881 Tab_Table_Win[6510:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
Correct answer is :
Thank you for all the explanation.
If an object doesn't exist for a key, NSDictionary will return
nil
. AnNSNull
is an actual object, and therefore a distinct thing. It's like the distinction between being able to record that there was a value and the value as null, and not recording whether there was a value. It also rests a bit on you thinking in C terms of the indirection of a pointer to an object rather than just an object, so it's not completely semantically pleasing from that perspective.In Objective-C, you may send any message to
nil
and the result is guaranteed to benil
(or 0). So if your code is designed to ensure that you have a safe object reference, as you might in C++, then what you're doing is unnecessary. Compound statements like:Are always explicitly safe, even if alloc fails and returns
nil
. All that'll happen is that the call toinit
won't do anything at all, and object will end up with the valuenil
because the result of sending ofinit
tonil
is alsonil
.That being said, the answers provided by Bill and Emmanuel should be correct. Compare your result either directly to
nil
or implicitly to zero. If you're getting a crash later on, I'll guess it's because you're expectingmyImageUrl
andmyImage
to be types other thanNSString
(I notice you've used the typelessid
in your original code) and sending them a message they don't respond to.When you call
objectForKey
in nullable dictionary, app gets crashed so I fixed this from this way to avoid from crash.}
that was not work for me, i figured it out like this
Whenever I try to check if an object being returned from a dictionary is null, I do this:
Then in your code, it would be:
That's what I would do in your code.
Also, just making sure, is the NSDictionary result defined? In your code, it doesn't have anything it's being set to. It's just being defined as variable you plan on using called results
this another option: