I’ve tried multiple ways. I know the dictionary is NULL, as the console also prints out when I break there. Yet when I put it in an if( ) it doesn’t trigger.
([myDict count] == 0) //results in crash
(myDict == NULL)
[myDict isEqual:[NSNull null]]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It looks like you have a dangling or wild pointer.
You can consider Objective-C objects as pointers to structs.
You can then of course compare them with
NULL, or with other pointers.So:
and
are both valid.
The first one will check if the pointer is
NULL.NULLis usually defined as avoid *with a value of 0.Note that, for Objective-C objects, we usually use
nil.nilis also defined as avoid *with a value of 0, so it equalsNULL. It’s just here to denote aNULLpointer to an object, rather than a standard pointer.The second one compares the address of
myDictwith the singleton instance of theNSNullclass. So you are here comparing two pointers values.So to quickly resume:
And as
[ NSNull null ]is a valid instance:Now about this:
It may crash if you have a wild pointer:
Unless using ARC, it will surely crash, because the
myDictvariable has not been initialised, and may actually point to anything.It may also crash if you have a dangling pointer:
Then you’ll try to send a message to a deallocated object.
Sending a message to
nil/NULLis always valid in Objective-C.So it depends if you want to check if a dictionary is
nil, or if it doesn’t have values (as a valid dictionary instance may be empty).In the first case, compare with
nil. Otherwise, checks if count is 0, and ensure you’re instance is still valid. Maybe you just forgot a retain somewhere.