NSString is empty

前端 未结 8 1858
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 05:45

How do you test if an NSString is empty? or all whitespace or nil? with a single method call?

8条回答
  •  后悔当初
    2021-01-30 05:56

    Maybe you can try something like this:

    + (BOOL)stringIsEmpty:(NSString *)str
    {
        return (str == nil) || (([str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]).length == 0);
    }
    

提交回复
热议问题