NSLog(@"%@", member.name); // prints (null) on UILabel
宏观:
NSLog(@"%@", SafeString(member.name)); // prints empty string on UILabel
干净利落
扩展解决方案(2020)
如果您更喜欢在项目中选择 nil Null 和空字符串,可以使用我的扩展行如下:
NSString + Extension.h
///
/// Checks if giving String is an empty string or a nil object or a Null.
/// @param string string value to check.
///
+ (BOOL)isNullOrEmpty:(NSString*)string;
NSString + Extension.m
+ (BOOL)isNullOrEmpty:(NSString*)string {
if (string) { // is not Nil
NSRange range = [string rangeOfString:string];
BOOL isEmpty = (range.length <= 0 || [string isEqualToString:@" "]);
BOOL isNull = string == (id)[NSNull null];
return (isNull || isEmpty);
}
return YES;
}
示例用法
if (![NSString isNullOrEmpty:someTitle]) {
// You can safely use on a Label or even add in an Array for example. Remember: Arrays don't like the nil values!
}