iOS开发常用方法

个人链接
  • 博客园主页 : 博客园主页
  • GitHub : 我的GitHub
  • iOS程序猿(媛)~~ : 这是我个人整理的一个技术专题, 这里的文章都是比较有技术含量(不断更新)!
  • 微信公众号 :
iOS开发常用方法_第1张图片
微信公众号.jpg

iOS开发中,常用判断的方法,希望大家可以记一下,肯定有用!

//获取字符串(或汉字)首字母

+ (NSString *)firstCharacterWithString:(NSString *)string{

NSMutableString *str = [NSMutableString stringWithString:string];

CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);

CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);

NSString *pingyin = [str capitalizedString];

return [pingyin substringToIndex:1];

}
//判断手机号码格式是否正确

+ (BOOL)valiMobile:(NSString *)mobile{

mobile = [mobile stringByReplacingOccurrencesOfString:@" "withString:@""];

if(mobile.length !=11)

{

returnNO;

}else{

/**

*移动号段正则表达式

*/

NSString *CM_NUM =@"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";

/**

*联通号段正则表达式

*/

NSString *CU_NUM =@"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";

/**

*电信号段正则表达式

*/

NSString *CT_NUM =@"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";

NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];

BOOL isMatch1 = [pred1 evaluateWithObject:mobile];

NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];

BOOL isMatch2 = [pred2 evaluateWithObject:mobile];

NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];

BOOL isMatch3 = [pred3 evaluateWithObject:mobile];

if(isMatch1 || isMatch2 || isMatch3) {

return YES;

}else{

return NO;

}

}

}
//判断注册邮箱是否正确

+ (BOOL)isAvailableEmail:(NSString *)email {

NSString *emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

return [emailTest evaluateWithObject:email];

}
//判断字符串中是否含有空格

+ (BOOL)isHaveSpaceInString:(NSString *)string{

NSRange _range = [string rangeOfString:@" "];

if(_range.location != NSNotFound) {

return YES;

}else{

return NO;

}

}

//判断字符串中是否含有某个字符串

+ (BOOL)isHaveString:(NSString *)string1 InString:(NSString *)string2{

NSRange _range = [string2 rangeOfString:string1];

if(_range.location != NSNotFound) {

return YES;

}else{

return NO;

}

}
//判断字符串中是否含有中文

+ (BOOL)isHaveChineseInString:(NSString *)string{

for(NSInteger i =0; i < [string length]; i++){

inta = [string characterAtIndex:i];

if(a >0x4e00&& a <0x9fff) {

return YES;

}

}

return NO;

}
//判断字符串是否全部为数字
+ (BOOL)isAllNum:(NSString *)string{
    unichar c;
    for(int i=0; i
//校验身份证号码(老的15位,新的18位)
- (BOOL)isAllIDcard:(NSString *)card{
    //15位
    NSString* number1=@"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
    NSPredicate * numberPre1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number1];
    BOOL isOK1 = [numberPre1 evaluateWithObject:card];
    
    //18位
    NSString* number2=@"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
    NSPredicate * numberPre2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number2];
    BOOL isOK2 = [numberPre2 evaluateWithObject:card];
    if(isOK1 || isOK2) {
        return YES;
    }
    return NO;
}
//检查数组,字典,字符串是否为空.
+(BOOL)isNilOfSender:(NSObject *)sender{
if(!sender) {
  returnYES;
}
if([sender isKindOfClass:[NSArray class]]) {
NSArray *array = (NSArray *)sender;
if(array.count) {
  return NO;
}else{
  return YES;
}
}
if([sender isKindOfClass:[NSDictionary class]]) {
NSDictionary *dic = (NSDictionary *)sender;
if([dic allKeys].count) {
  return NO;
}else{
  return YES;
}
}
if([sender isKindOfClass:[NSString class]]) {
NSString *str = (NSString *)sender;
if(str !=NULL&& [str stringByReplacingOccurrencesOfString:@" "withString:@""].length >0) {
  return NO;
}
else{
  return YES;
  }
}
return YES;
}
声明
  • 所有文章出自 Kenny Hito 的博客 !
  • 未经本人允许不得转载, 转载请标明来源与作者, 谢谢合作!

你可能感兴趣的:(iOS开发常用方法)