如何测试Objective C中的字符串是否为空?
如何测试Objective C中的NSString是否为空? 
  你可以检查是否[string length] == 0 。  这将检查它是否是一个有效的空字符串(@“”)以及是否为零,因为nil上的调用length也将返回0。 
  马克的回答是正确的。  但我会借此机会向Wil Shipley的广义isEmpty提供一个指示,他在他的博客上分享了这个指示: 
static inline BOOL IsEmpty(id thing) {
return thing == nil
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}
  第一种方法是有效的,但如果你的字符串有空格( @" " )则不起作用。  所以你必须在测试之前清除这个空白区域。 
该代码清除字符串两边的所有空格:
[stringObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
一个好主意是创建一个宏,所以你不必输入这个怪物行:
#define allTrim( object ) [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]
现在你可以使用:
NSString *emptyString = @"   ";
if ( [allTrim( emptyString ) length] == 0 ) NSLog(@"Is empty!");
