iOS开发空字符串解决方法

-(Boolean) isEmptyOrNull:(NSString *) str {
    if (!str) {
        // null object
        return true;
    }else if(str == Null){
return true;
}else if([str isKindOfClass:[NSNull class]]){
        return true;
    }else {
        NSString *trimedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([trimedString length] == 0) {
            // empty string
            return true;
        } else {
            // is neither empty nor null
            return false;
        }
    }
}


str可能为nil,也可能为NSNull class,也可能length为0.
如果直接使用最后else中的语句,在为NSNull的情况下是会报NSNull length缺少selector之类的异常

猜你喜欢

转载自lizhuang.iteye.com/blog/2015649