What is the right way to check for a null string in Objective-C?
As others have pointed out, there are many kinds of "null" under Cocoa/Objective C. But one further thing to note is that [title 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 a good test might be:
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.
How do I test if a string is empty in Objective-C?
You can check if [string length] == 0
. This will check if it's a valid but empty string (@"") as well as if it's nil, since calling length
on nil will also return 0.
How to check the NULL value in NSString in iOS?
In Objective-C and Cocoa, the property may not be set—that is, it's nil
—or it may be set to the object representation of nil
, which is an instance of NSNull
. You probably want to check for either of these conditions, like this:
NSString* categoryName = appDelegate.categoryName;
if (categoryName == nil || categoryName == (id)[NSNull null]) {
// nil branch
} else {
// category name is set
}
This will execute the nil branch if the categoryName
property is set to nil
(the default for all properties), or if it's been explicitly set to the NSNull
singleton.
Objective-C how to check if a string is null
Your first warning looks like you're trying to call objectForKey
on an NSArray
. Which isn't going to work, as NSArray
doesn't have an objectForKey
method.
As for the second warning you can just compare directly with nil, ie:
if (temp != nil)
or since nil is equivalent to 0, you can also just do:
if (temp)
Checking a null value in Objective-C that has been returned from a JSON string
is how the NSNull singleton logs. So:
if (tel == (id)[NSNull null]) {
// tel is null
}
(The singleton exists because you can't add nil
to collection classes.)
how to Check NSString is null or not
Like that:
[myString isEqual: [NSNull null]];
NSString nil or empty check -- is this complete?
I only use the next conditional and do not even need a category:
if (!aString.length)
{
...
}
Using Objective-C theory, a message to NIL will return nil or zero, so basically you do not have to test for nil.
How to check if an object is nil in Objective-C?
You can do it normally just by checking the object itself. There is also a good explanation on NSHipster for NSNull.
if( myObject ){
// do something if object isn't nil
} else {
// initialize object and do something
}
otherwise just use
if( myObject == nil ){
}
null, nill is string type in Objective-C
There are several values that are different from Objective-C perspective here:
nil
(akanull
in other languages)[NSNull null]
(a special marker value object)@""
(empty string)@"null"
(just a string with 4 characters)
If you write your dataDic
from your app, and you know that your app handles it well, you don't have to check all the cases. Check only the ones you expect. For example, if your app only writes non-empty strings to the dictionary, but sometimes "email" is not there, you only have to check nil
, because objectForKey
returns nil
if the value is not inside the dictionary.
On the other hand if you have obtained dataDic
from a 3rd party API, decoded from JSON for example, then you should do the full checking:
- [NSNull null] is placed inside the dictionary if JSON has
null
originally like{"email":null}
- If the server API changed you might get some other structure than NSString there (although quite unlikely here).
nil
is returned if you don't have the key/value at all.
You can rule out all the 3 checks at once by doing:
NSString *emailStr = nil;
id emailObj = [dataDic objectForKey:@"email"];
if ([emailObj isKindOfClass:[NSString class]]) {
emailStr = emailObj;
}
Note that you might not have to check for an empty string or nil before assigning to UILabel text, because those work fine and just erase the label text:
self.emailLbl.text = @"";
self.emailLbl.text = nil; // another way to erase
Related Topics
Core Data Background Context Best Practice
iOS 5 Twitter Framework: Tweeting Without User Input and Confirmation (Modal View Controller)
Why Is Main Window of Type Double Optional
Store [String] in Nsuserdefaults
Uigraphicsgetimagefromcurrentimagecontext Memory Leak with Previews
How to Fix Xcode "Dtassetproviderservice Could Not Start.." Error
Custom Annotation View for Userlocation Not Moving the Mapview
Core Data - Failed to Load Optimized Model at Path
Example of Nsattributedstring with Two Different Font Sizes
Dyld: Symbol Not Found: _Nsurlauthenticationmethodclientcertificate When Trying to Run iOS App
How to Run/Test My Flutter App on a Real Device
No Umbrella Header Found for Target, Module Map Will Not Be Generated
Ios: Sample Code for Simultaneous Record and Playback
Google Sign-In Crashes on iOS 9 Attempting to Call Canopenurl
Inconsistent Unicode Emoji Glyphs/Symbols
Receiver (<Viewcontroller>) Has No Segue with Identifier 'Addsegue'