美文网首页
iOS一些常用的工具类

iOS一些常用的工具类

作者: 何色相撩我心弦 | 来源:发表于2019-03-01 10:52 被阅读0次

    工作中经常会用到的一些便捷方法,收藏以备不时之需。


    //获取当前时间戳有两种方法(以秒为单位)

    //获取当前时间戳有两种方法(以秒为单位)

    +(NSString*)getNowTimeTimestampWithNowDate:(NSDate*)nowDate{

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;

        [formattersetDateStyle:NSDateFormatterMediumStyle];

        [formattersetTimeStyle:NSDateFormatterShortStyle];

        [formattersetDateFormat:@"YYYY-MM-dd HH:mm:ss"];

        NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/BeiJing"];

        [formattersetTimeZone:timeZone];

    //    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式

        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[nowDate timeIntervalSince1970]];

    //    NSLog(@"-------------1111---timeSp:%@",timeSp); //时间戳的值时间戳转时间的方法

        returntimeSp;

    }



    //获取当前的时间

    //获取当前的时间

    +(NSString*)getCurrentTimesWithNowDate:(NSDate*)nowDate

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        [formattersetDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    //    NSDate *datenow = [NSDate date];

            NSString*currentTimeString = [formatterstringFromDate:nowDate];

        returncurrentTimeString;

    }



    //检查字符非空 nil null @“” <null>

    //检查字符非空 nil null @“” <null>

    +(BOOL)judgeBlankString:(NSString*)aStr {

        if(!aStr) {

            returnYES;

        }

        if ([aStr isKindOfClass:[NSNull class]]) {

            returnYES;

        }

        if(!aStr.length) {

            returnYES;

        }

        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];

        NSString *trimmedStr = [aStr stringByTrimmingCharactersInSet:set];

        if(!trimmedStr.length) {

            returnYES;

        }

        return NO;

    }



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

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

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

        mobile = [mobilestringByReplacingOccurrencesOfString:@" " 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];

            BOOLisMatch1 = [pred1evaluateWithObject:mobile];

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

            BOOLisMatch2 = [pred2evaluateWithObject:mobile];

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

            BOOLisMatch3 = [pred3evaluateWithObject:mobile];

            if(isMatch1 || isMatch2 || isMatch3) {

                returnYES;

            }else{

                returnNO;

            }

        }

    }


    /*限制输入数字*/

    /*限制输入数字*/

    + (BOOL)validateNumber:(NSString*)number {

        BOOLres =YES;

        NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

        inti =0;

        while(i < number.length) {

            NSString * string = [number substringWithRange:NSMakeRange(i, 1)];

            NSRangerange = [stringrangeOfCharacterFromSet:tmpSet];

            if(range.length==0) {

                res =NO;

                break;

            }

            i++;

        }

        returnres;

    }

    //正则判断全是数字

    //正则判断全是数字

    +(BOOL)judgeNumInputShouldNumber:(NSString*)str{

        if(str.length==0) {

            returnNO;

        }

        NSString*regex =@"[0-9]*";

        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];

        if([predevaluateWithObject:str]) {

            returnYES;

        }

        return NO;

    }

    /*过滤所有空格*/

    /*过滤所有空格*/

    + (NSString*)stringContainCharactersInSet:(NSString*)str{

        NSString *topstr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        NSString *endStr = [topstr stringByReplacingOccurrencesOfString:@" " withString:@""];

        returnendStr;

    }

    /*过滤emoji*/

    /*过滤emoji*/

    +(BOOL)stringContainsEmoji:(NSString*)string

    {

        // 过滤所有表情。returnValue为NO表示不含有表情,YES表示含有表情

        __blockBOOLreturnValue =NO;

        [stringenumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

            constunicharhs = [substringcharacterAtIndex:0];

            // surrogate pair

            if(0xd800<= hs && hs <=0xdbff) {

                if(substring.length>1) {

                    constunicharls = [substringcharacterAtIndex:1];

                    constintuc = ((hs -0xd800) *0x400) + (ls -0xdc00) +0x10000;

                    if(0x1d000<= uc && uc <=0x1f77f) {

                        returnValue =YES;

                    }

                }

            }elseif(substring.length>1) {

                constunicharls = [substringcharacterAtIndex:1];

                if(ls ==0x20e3) {

                    returnValue =YES;

                }

            }else{

                // non surrogate

                if(0x2100<= hs && hs <=0x27ff) {

                    returnValue =YES;

                }elseif(0x2B05<= hs && hs <=0x2b07) {

                    returnValue =YES;

                }elseif(0x2934<= hs && hs <=0x2935) {

                    returnValue =YES;

                }elseif(0x3297<= hs && hs <=0x3299) {

                    returnValue =YES;

                }elseif(hs ==0xa9|| hs ==0xae|| hs ==0x303d|| hs ==0x3030|| hs ==0x2b55|| hs ==0x2b1c|| hs ==0x2b1b|| hs ==0x2b50) {

                    returnValue =YES;

                }

            }

        }];

        returnreturnValue;

    }

    //过滤特殊字符

    //过滤特殊字符

    + (NSString*)encodeToPercentEscapeString:(NSString*)input

    {

        NSString*outputStr = (NSString*)

        CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,

                                                                  (CFStringRef)input,

                                                                  NULL,

                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",

                                                                  kCFStringEncodingUTF8));

        returnoutputStr;

    }

    相关文章

      网友评论

          本文标题:iOS一些常用的工具类

          本文链接:https://www.haomeiwen.com/subject/pvwruqtx.html