NSString is empty

前端 未结 8 1851
伪装坚强ぢ
伪装坚强ぢ 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:54

    Based on the Jacob Relkin answer and Jonathan comment:

    @implementation TextUtils
    
        + (BOOL)isEmpty:(NSString*) string {
    
            if([string length] == 0 || ![[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) {
                return YES;
            }
    
            return NO;
        }
    
        @end
    

提交回复
热议问题