常用方法 - iOS

收集了常用的方法,做个记录

#pragma mark -  json转换
+(id )getObjectFromJsonString:(NSString *)jsonString
{
NSError *error = nil;
if (jsonString) {
    id rev=[NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUnicodeStringEncoding] options:NSJSONReadingMutableLeaves error:&error];
    if (error==nil) {
        return rev;
    }
    else
    {
        return nil;
    }
}
return nil;
}

+(NSString *)getJsonStringFromObject:(id)object
{
if ([NSJSONSerialization isValidJSONObject:object])
    
{
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:0 error:nil];
    
    
    
    return [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

return nil;
}

#pragma mark -  NSDate互转NSString
+(NSDate *)NSStringToDate:(NSString *)dateString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
return dateFromString;
}

+(NSDate *)NSStringToDate:(NSString *)dateString withFormat:(NSString *)formatestr{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatestr];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
return dateFromString;
}

+(NSString *)NSDateToString:(NSDate *)dateFromString withFormat:(NSString *)formatestr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatestr];
NSString *strDate = [dateFormatter stringFromDate:dateFromString];
return strDate;
}



#pragma mark -  判断字符串是否为空,为空的话返回 “” (一般用于保存字典时)
+(NSString *)IsNotNull:(id)string
{
NSString * str = (NSString*)string;
if ([self isBlankString:str]){
    string = @"";
}
return string;

}

//..判断字符串是否为空字符的方法
+(BOOL) isBlankString:(id)string {
NSString * str = (NSString*)string;
if ([str isEqualToString:@"(null)"]) {
    return YES;
}
if (str == nil || str == NULL) {
    return YES;
}
if ([str isKindOfClass:[NSNull class]]) {
    return YES;
}
if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
    return YES;
}
return NO;
}


#pragma mark - 使用subString去除float后面无效的0
+(NSString *)changeFloatWithString:(NSString *)stringFloat
{
const char *floatChars = [stringFloat UTF8String];
NSUInteger length = [stringFloat length];
NSUInteger zeroLength = 0;
NSInteger i = length-1;
for(; i>=0; i--)
{
    if(floatChars[i] == '0') {
        zeroLength++;
    } else {
        if(floatChars[i] == '.')
            i--;
        break;
    }
}
NSString *returnString;
if(i == -1) {
    returnString = @"0";
} else {
    returnString = [stringFloat substringToIndex:i+1];
}
return returnString;
  }

#pragma mark - 去除float后面无效的0
+(NSString *)changeFloatWithFloat:(CGFloat)floatValue

{
return [self changeFloatWithString:[NSString stringWithFormat:@"%f",floatValue]];
}

#pragma mark - 如何通过一个整型的变量来控制数值保留的小数点位数。以往我们通类似@"%.2f"来指定保留2位小数位,现在我想通过一个变量来控制保留的位数
+(NSString *)newFloat:(float)value withNumber:(int)numberOfPlace
{
NSString *formatStr = @"%0.";
formatStr = [formatStr stringByAppendingFormat:@"%df", numberOfPlace];
NSLog(@"____%@",formatStr);

formatStr = [NSString stringWithFormat:formatStr, value];
NSLog(@"____%@",formatStr);

printf("formatStr %s\n", [formatStr UTF8String]);
return formatStr;
}


#pragma mark -  手机号码验证
+(BOOL) isValidateMobile:(NSString *)mobile
{
/*
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
*/

NSPredicate* phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"1[34578]([0-9]){9}"];

return [phoneTest evaluateWithObject:mobile];

}

#pragma mark -  阿里云压缩图片
+(NSURL*)UrlWithStringForImage:(NSString*)string{
NSString * str = [NSString stringWithFormat:@"%@@800w_600h_10Q.jpg",string];
NSLog(@"加载图片地址=%@",str);
return [NSURL URLWithString:str];
}

//..去掉压缩属性“@800w_600h_10Q.jpg”
+(NSString*)removeYaSuoAttribute:(NSString*)string{
NSString * str = @"";
if ([string rangeOfString:@"@"].location != NSNotFound) {
    NSArray * arry = [string componentsSeparatedByString:@"@"];
    str = arry[0];
}
return str;
}

#pragma mark - 字符串类型判断
//..判断是否为整形:
+ (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
}

//判断是否为浮点形:
+ (BOOL)isPureFloat:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
}


#pragma mark -  计算内容文本的高度方法
+ (CGFloat)HeightForText:(NSString *)text withSizeOfLabelFont:(CGFloat)font withWidthOfContent:(CGFloat)contentWidth
{
NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
CGSize size = CGSizeMake(contentWidth, 2000);
CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
return frame.size.height;
}

#pragma mark -  计算字符串长度
+ (CGFloat)WidthForString:(NSString *)text withSizeOfFont:(CGFloat)font
{
NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
CGSize size = [text sizeWithAttributes:dict];
return size.width;
}



#pragma mark -  计算两个时间相差多少秒

+(NSInteger)getSecondsWithBeginDate:(NSString*)currentDateString  AndEndDate:(NSString*)tomDateString{

NSDate * currentDate = [XYString NSStringToDate:currentDateString withFormat:@"yyyy-MM-dd HH:mm:ss"];
NSInteger currSec = [currentDate timeIntervalSince1970];

NSDate *tomDate = [XYString NSStringToDate:tomDateString withFormat:@"yyyy-MM-dd HH:mm:ss"];
NSInteger tomSec = [tomDate timeIntervalSince1970];

NSInteger newSec = tomSec - currSec;
NSLog(@"相差秒:%ld",(long)newSec);
return newSec;
}


#pragma mark - 根据出生日期获取年龄
+ (NSInteger)ageWithDateOfBirth:(NSDate *)date;
{
// 出生日期转换 年月日
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
NSInteger brithDateYear  = [components1 year];
NSInteger brithDateDay   = [components1 day];
NSInteger brithDateMonth = [components1 month];

// 获取系统当前 年月日
NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger currentDateYear  = [components2 year];
NSInteger currentDateDay   = [components2 day];
NSInteger currentDateMonth = [components2 month];

// 计算年龄
NSInteger iAge = currentDateYear - brithDateYear - 1;
if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
    iAge++;
}

return iAge;
}


#pragma mark - 根据经纬度计算两个位置之间的距离
+(double)distanceBetweenOrderBylat1:(double)lat1 lat2:(double)lat2 lng1:(double)lng1 lng2:(double)lng2{
double dd = M_PI/180;
double x1=lat1*dd,x2=lat2*dd;
double y1=lng1*dd,y2=lng2*dd;
double R = 6371004;
double distance = (2*R*asin(sqrt(2-2*cos(x1)*cos(x2)*cos(y1-y2) - 2*sin(x1)*sin(x2))/2));
//返回km
return  distance/1000;

//返回m
//return   distance;

}

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