How to check null value of object in decision statement for Objective C - iphone

I am getting a object value from server as null value when NSlog this object.I want to identify it in if-else decision statement. How can I check it because nil have reference to a unknown object which not means NULL.and i can't compare it with zero too.
How can i identify that this value is NULL, i have a crash on this point.I have tried #try - #catch block too but all gone in vain.
Any suggestion for this problem.

As others have pointed out, there are many kinds of "null" under Cocoa/Objective C.
But one further thing to note is that [object isKindOfClass:[NSNull class]] is pointlessly complex since [NSNull null] is documented to be a singleton so you can just check for pointer equality. See Topics for Cocoa: Using Null
So use this :-
if (title == (id)[NSNull null] || title.length == 0 ) title = #"Something";
Note how you can use the fact that even if title is nil, title.length will return 0/nil/false, ie 0 in this case, so you do not have to special case it. This is something that people who are new to Objective C have trouble getting used to, especially coming form other languages where messages/method calls to nil crash.
If you want in detail what is the difference between nil, Nil and null, you can check this article What is the difference between nil, Nil and null.

You can try following code to check for NULL values from server:
if (nil == str || NSNull.null == (id)str) {
//Object has Null value
}
else{
// Object has some value
}
str is string value which contain server value.
This may helps you.

The Best Approach is :
if([yourObject isKindOfClass:[NSNull null]])
{
// yourObject is null.
}
else
{
// yourObject is not null.
}

Related

Check if NSDictionary is Null?

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]]
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:
( myDict == NULL )
and
( myDict == [ NSNull null ] )
are both valid.
The first one will check if the pointer is NULL. NULL is usually defined as a void * with a value of 0.
Note that, for Objective-C objects, we usually use nil. nil is also defined as a void * with a value of 0, so it equals NULL. It's just here to denote a NULL pointer to an object, rather than a standard pointer.
The second one compares the address of myDict with the singleton instance of the NSNull class. So you are here comparing two pointers values.
So to quickly resume:
NULL == nil == Nil == 0
And as [ NSNull null ] is a valid instance:
NULL != [ NSNull null ]
Now about this:
( [ myDict count ] == 0 )
It may crash if you have a wild pointer:
NSDictionary * myDict;
[ myDict count ];
Unless using ARC, it will surely crash, because the myDict variable has not been initialised, and may actually point to anything.
It may also crash if you have a dangling pointer:
NSDictionary * myDict;
myDict = [ [ NSDictionary alloc ] init ];
[ myDict release ];
[ myDict count ];
Then you'll try to send a message to a deallocated object.
Sending a message to nil/NULL is 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.
if (TheDict == (NSDictionary*) [NSNull null]){
//TheDict is null
}
else{
//TheDict is not null
}
To check if a dictionary has nil or NULL data, you can check for the [dictionary count] which will return 0 in all cases
if([NSNull null] != [serverResponseObject objectForKey:#"Your_Key"])
{
//Success
}
All above doesn't work for me but this
if([mydict isKindOfClass:[NSNull class]])
{
NSLog("Dic is Null")
}
else
{
NSLog("Dic is Not Null")
}
Worked for me

NSString or NSCFString in xcode?

I m taking a NSMutabledictionary object in NSString like this :
NSString *state=[d valueForKey:#"State"];
Now sometimes state may be null and sometimes filled with text.So Im comparing it.While comparing state becomes NSString sometimes and NSCFString othertimes..So unable to get the desired result..
if([state isEqualToString#""])
{
//do something
}
else
{
//do something
}
So while comparing it is returning nil sometimes.So immediately jumping into the else block.
I need a standard way to compare if the state is empty whether it is a NSString or NSCFString ...
How can I do it?
If you're unable to get the result you want, I can assure you it's not because you get a NSCFString instead of a NSString.
In Objective-C, the framework is filled with cluster classes; that is, you see a class in the documentation, and in fact, it's just an interface. The framework has instead its own implementations of these classes. For instance, as you noted, the NSString class is often represented by the NSCFString class instead; and there are a few others, like NSConstantString and NSPathStore2, that are in fact subclasses of NSString, and that will behave just like you expect.
Your issue, from what I see...
Now sometimes state may be null and sometimes filled with text.
... is that in Objective-C, it's legal to call a method on nil. (Nil is the Objective-C concept of null in other languages like C# and Java.) However, when you do, the return value is always zeroed; so if you string is nil, any equality comparison to it made with a method will return NO, even if you compare against nil. And even then, please note that an empty string is not the same thing as nil, since nil can be seen as the absence of anything. An empty string doesn't have characters, but hey, at least it's there. nil means there's nothing.
So instead of using a method to compare state to an empty string, you probably need to check that state is not nil, using simple pointer equality.
if(state == nil)
{
//do something
}
else
{
//do something
}
You can do this
if([state isEqualToString:#""])
{
//do something
}
else
{
//do something
}
You must have to type cast it to get the correct answer.
NSString *state = (NSString *) [d valueForKey:#"State"];
if(state != nil)
{
if(state.length > 0)
{
//string contains characters
}
}

iPhone : Difference between nil vs Nil and true vs TRUE

What is difference between nil and Nil in iOS development?
And similarly what is difference between true and TRUE in iOS development?
I think this will help you understand the difference between nil and Nil.
Please find the below link:
Refer the answer of Damien_The_Unbeliever which states:
Googling "Nil vs nil" found this post http://numbergrinder.com/node/49, which states:
All three of these values represent null, or zero pointer, values. The
difference is that while NULL represents zero for any pointer, nil is
specific to objects (e.g., id) and Nil is specific to class pointers.
It should be considered a best practice of sorts to use the right null
object in the right circumstance for documentation purposes, even
though there is nothing stopping someone from mixing and matching as
they go along.
Link for that answer can be seen here:
What does 'Nil' represent in Obj-C?
EDIT-2:
Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?
nil is the literal null value for Objective-C objects, corresponding to the abstract type id or any Objective-C type declared via #interface. For instance:
NSString *someString = nil;
NSURL *someURL = nil;
id someObject = nil;
if (anotherObject == nil) // do something
Nil is the literal null value for Objective-C classes, corresponding to the type Class. Since most code doesn’t need variables to reference classes, its use is not common. One example is:
Class someClass = Nil;
Class anotherClass = [NSString class];

If UITextField or NSString is empty

If I want to check whether a UITextField or NSString is empty, can I compare it with NULL or nil?
Neither of the methods you suggest are foolproof. The best tests are:
if ([myTextField.text length] > 0) ...
or
if ([myString length] > 0) ...
if i want to check whether a textfield or string is empty i compare it with NULL or nil?
No.
An empty string object (a string object containing no characters) or a text-field object containing an empty string object is not the same as nil, which is no object at all. You need to ask the (text field's) string how long it is, or ask it whether it is equal to an empty string you have on hand (#"").
NULL, while also a null pointer, should be used for general pointers, not pointers to Objective-C instances (for which you have the more specific nil) or classes (for which you have the more specific Nil).
I had a similar problem but no method other than this worked for me:
NSString *string = textfield.text;
if ([string isEqualToString:#""]) {
....
}

How to check in an NSAssert of an variable is (null)?

the console prints me (null) for an variable that should not be, so I want to put an assert there just for fun:
NSAssert(myVar, #"myVar was (null)!");
what's the trick here?
this (null) thing doesn't seem to be "nil", right? How can I check for it? I want to assume that the var is set properly and not nil, not null.
Assuming myVar is of type id (or any object type), use
NSAssert(myVar != nil, #"")
If the assertion passes, you should be able to print myVar to the console with
NSLog(#"myVar = %#", myVar);
As a side note, nil==null, (both are #defined as __DARWIN_NULL which is #defined ((void *)0)). If you try to use NSLog to print the -description of a nil (or NULL) object reference you get "(null)"
do [myVar isEqualToClass:[NSNull null]] this returns yes if it is NSNull, if its nil you can do if(myVar==nil)
I usually do
if (object != nil)
doSomething(object);
else
NSLog("bad object!");