工作中經(jīng)常會用到的一些便捷方法琅轧,收藏以備不時之需。
//獲取當(dāng)前時間戳有兩種方法(以秒為單位)
//獲取當(dāng)前時間戳有兩種方法(以秒為單位)
+(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];//現(xiàn)在時間,你可以輸出來看下是什么格式
? ? NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[nowDate timeIntervalSince1970]];
//? ? NSLog(@"-------------1111---timeSp:%@",timeSp); //時間戳的值時間戳轉(zhuǎn)時間的方法
? ? returntimeSp;
}
//獲取當(dāng)前的時間
//獲取當(dāng)前的時間
+(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;
}
//判斷手機(jī)號碼格式是否正確
//判斷手機(jī)號碼格式是否正確
+ (BOOL)judgeMobile:(NSString*)mobile{
? ? mobile = [mobilestringByReplacingOccurrencesOfString:@" " withString:@""];
? ? if(mobile.length!=11)
? ? {
? ? ? ? returnNO;
? ? }else{
? ? ? ? /**
?? ? ? ? * 移動號段正則表達(dá)式
?? ? ? ? */
? ? ? ? NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
? ? ? ? /**
?? ? ? ? * 聯(lián)通號段正則表達(dá)式
?? ? ? ? */
? ? ? ? NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
? ? ? ? /**
?? ? ? ? * 電信號段正則表達(dá)式
?? ? ? ? */
? ? ? ? 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;
? ? ? ? }
? ? }
}
/*限制輸入數(shù)字*/
/*限制輸入數(shù)字*/
+ (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;
}
//正則判斷全是數(shù)字
//正則判斷全是數(shù)字
+(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;
}