#pragma mark 获取设备id
+ (NSString *)getDeviceId
{
NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
return identifierForVendor;
}
#pragma mark 判断字符串是否为空,null,nil等...(YES:为空)
+ (BOOL)valString:(NSString *)string {
NSString *toString = [NSString stringWithFormat:@"%@",string];
if (toString == nil || toString == NULL) {
return YES;
}
if ([toString isKindOfClass:[NSNull class]]) {
return YES;
}
if ([toString isEqualToString:@""]) {
return YES;
}
if ([toString isEqualToString:@"(null)"]) {
return YES;
}
if ([toString isEqualToString:@"<null>"]) {
return YES;
}
if ([toString stringByReplacingOccurrencesOfString:@" " withString:@""].length<=0) {
return YES;
}
return NO;
}
#pragma mark 判断字符串是否是数字 是返回本身不是返回0
+ (NSString *)stringTurnNumber:(NSString *)string {
NSString *toString = [[NSString stringWithFormat:@"%@",string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([self isNumText:toString]) {
return toString;
}else{
return @"0";
}
}
#pragma mark 判断字符串是否为空,空返回@""不为空返回本身
+ (NSString *)stringTurnString:(NSString *)string {
if ([self valString:string]) {
return @"";
}else{
return [NSString stringWithFormat:@"%@",string];
}
}
#pragma mark 是否是纯数字
+ (BOOL)isNumText:(NSString *)str {
str = [NSString stringWithFormat:@"%@",str];
NSScanner* scan = [NSScanner scannerWithString:str];
double val;
return[scan scanDouble:&val] && [scan isAtEnd];
}
#pragma mark 判断输入的密码是否是数字和字母组成
+(BOOL)isPassword:(NSString *)string{
NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"];
s = [s invertedSet];
NSRange r = [string rangeOfCharacterFromSet:s];
if (r.location !=NSNotFound) {
return NO;
}
return YES;
}
#pragma mark 验证身份证号码
+ (BOOL) IsIdentityCard:(NSString *)IDCardNumber
{
if (IDCardNumber.length <= 0) {
return NO;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:IDCardNumber];
}
#pragma mark 验证手机号
+ (BOOL) IsPhoneNumber:(NSString *)number
{
NSString *phoneRegex1=@"1[34578]([0-9]){9}";
NSPredicate *phoneTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex1];
return [phoneTest1 evaluateWithObject:number];
}
#pragma mark 验证银行卡
+ (BOOL) IsBankCard:(NSString *)cardNumber
{
if(cardNumber.length==0)
{
return NO;
}
NSString *digitsOnly = @"";
char c;
for (int i = 0; i < cardNumber.length; i++)
{
c = [cardNumber characterAtIndex:i];
if (isdigit(c))
{
digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
}
}
int sum = 0;
int digit = 0;
int addend = 0;
BOOL timesTwo = false;
for (NSInteger i = digitsOnly.length - 1; i >= 0; i--)
{
digit = [digitsOnly characterAtIndex:i] - '0';
if (timesTwo)
{
addend = digit * 2;
if (addend > 9) {
addend -= 9;
}
}
else {
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}
int modulus = sum % 10;
return modulus == 0;
}
#pragma mark - NSIndexPath 根据TableView、Cell子视图,获取indexPath
+ (NSIndexPath *)indexPathWithTableView:(UITableView *)tableView cellSubview:(UIView *)view {
if (![tableView isKindOfClass:[UITableView class]]) return nil;
if (![view isKindOfClass:[UIView class]]) return nil;
if ([view isKindOfClass:[UITableViewCell class]]) return [tableView indexPathForCell:(UITableViewCell *)view];
id cell = view;
while (YES) {
cell = [cell superview];
if ([cell isKindOfClass:[UITableViewCell class]]) return [tableView indexPathForCell:(UITableViewCell *)cell];
if (![cell isKindOfClass:[UIView class]]) return nil;
}
return nil;
}