How do I test if an NSString
is empty in Objective C?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
I put this:
The problem is that if self is nil, this function is never called. It'll return false, which is desired.
The first approach is valid, but doesn't work if your string has blank spaces (
@" "
). So you must clear this white spaces before testing it.This code clear all the blank spaces on both sides of the string:
One good idea is create one macro, so you don't have to type this monster line:
Now you can use:
Its as simple as
if([myString isEqual:@""])
orif([myString isEqualToString:@""])
Just use one of the
if
else
conditions as shown below:Method 1:
Method 2:
Simply Check your string length
a message to NIL will return nil or 0, so no need to test for nil :).
Happy coding ...
Marc's answer is correct. But I'll take this opportunity to include a pointer to Wil Shipley's generalized
isEmpty
, which he shared on his blog: