EFUtils示例(1)-验证值是否合法

验证值是否合法 Validate the value

+objectIsNilOrNull:

对象判空。(Validate the object is nil or null.)

NSObject *object; // Nil
LOG_FORMAT(@"1: %d", [EFUtils objectIsNilOrNull:object]);
object = [NSNull null]; // Null
LOG_FORMAT(@"2: %d", [EFUtils objectIsNilOrNull:object]);
object = [NSObject alloc]; // Object alloc
LOG_FORMAT(@"3: %d", [EFUtils objectIsNilOrNull:object]);

if (![EFUtils objectIsNilOrNull:object]) { // 不为空
    // do some thing here
}

+stringIsNilOrNullOrEmpty:

字符串判空。(Validate the string is nil or null or empty.)

NSString *string; // Nil
LOG_FORMAT(@"1: %d", [EFUtils stringIsNilOrNullOrEmpty:string]);
string = (NSString *)[NSNull null]; // Null
LOG_FORMAT(@"2: %d", [EFUtils stringIsNilOrNullOrEmpty:string]);
string = @""; // String with a empty string
LOG_FORMAT(@"3: %d", [EFUtils stringIsNilOrNullOrEmpty:string]);

if (![EFUtils stringIsNilOrNullOrEmpty:string]) { // 不为空
    // do some thing here
}

+validateString:byRegExp:

使用正则表达式验证字符串值。(Validate the string by a regular expression.)

// AppUtils
/**
 检查用户名是否合法

 @param userName 用户名,是一个手机号码
 @return 检查结果,YES 合法 NO 不合法
 */
+ (BOOL)checkUserName:(NSString *_Nonnull)userName {
    return [EFUtils validateString:userName byRegExp:@"^1\\d{10}$"];
}

// Test
LOG_FORMAT(@"1: %d", [AppUtils checkUserName:@"123456"]);
LOG_FORMAT(@"2: %d", [AppUtils checkUserName:@"01234567890"]);
LOG_FORMAT(@"3: %d", [AppUtils checkUserName:@"13678234511"]);

if (![AppUtils checkUserName:userName]) { // 不合法
    LOG_FORMAT(@"用户名是一个手机号码,请检查");
    return;
}

+objectValueIsNilOrNull:withKey:

字典中指定键值判空。(Validate the key value in a dictionary is nil or null.)

NSDictionary *dictionary = @{@"ID": @59,
                           @"name": @"James",
                            @"age": [NSNull null],
                        @"offline": @true};
LOG_FORMAT(@"1: %d", [EFUtils objectValueIsNilOrNull:dictionary withKey:@"name"]);
LOG_FORMAT(@"2: %d", [EFUtils objectValueIsNilOrNull:dictionary withKey:@"age"]);
LOG_FORMAT(@"3: %d", [EFUtils objectValueIsNilOrNull:dictionary withKey:@"nick"]);

if (![EFUtils objectValueIsNilOrNull:dictionary withKey:@"nick"]) { // 不为空
    // do some thing here
}

+objectValueIsEqualTo:dictionary:withKey:

字典中指定键值是否等于指定数值。(Validate the key value in a dictionary is equal to another integer value in a NSNumber or NSString object or not.)

NSDictionary *dictionary = @{@"status": @200,
                                 @"ID": @59,
                               @"name": @"James",
                                @"age": [NSNull null],
                            @"offline": @true};
LOG_FORMAT(@"1: %d", [EFUtils objectValueIsEqualTo:@"00000059" dictionary:dictionary withKey:@"ID"]);
LOG_FORMAT(@"2: %d", [EFUtils objectValueIsEqualTo:@YES dictionary:dictionary withKey:@"offline"]);
LOG_FORMAT(@"3: %d", [EFUtils objectValueIsEqualTo:@1 dictionary:dictionary withKey:@"offline"]);

if ([EFUtils objectValueIsEqualTo:@200 dictionary:dictionary withKey:@"status"]) { // 请求成功
    // do some thing here
}

+validateDictionary:

字典中所有的值都不为空,注意之前 +validDictionary: 返回的值已发生反转。(Validate all values in a dictionary is all not empty, note that the returned value from +validDictionary: has been reversed.)

NSDictionary *dictionary = @{@"ID": @59,
                           @"name": @"James",
                            @"age": [NSNull null],
                        @"offline": @true};
LOG_FORMAT(@"%d", [EFUtils validateDictionary:dictionary]);

if ([EFUtils validateDictionary:dictionary]) { // 返回数据均不为空
    // do some thing here
}

相关

  • 详见极致框架官网中的介绍。通过极致框架官网顶部的搜索功能搜索 EFUtils。

许可

  • 本文采用 BY-NC-SA 许可协议。即:署名——转载请注明出处;非商业使用;相同方式传播——再分发的文章许可与原文相同。

查看原文

你可能感兴趣的:(EFUtils示例(1)-验证值是否合法)