美文网首页
iOS-常用工具方法

iOS-常用工具方法

作者: 天码行空 | 来源:发表于2017-04-17 15:24 被阅读0次

    解析JSON字符串

    -(id)toArrayOrNSDictionary:(NSString *)jsonStr{

    NSData *jsonData = [jsonStr dataUsingEncoding:NSASCIIStringEncoding];

    NSError *error = nil;

    id jsonObject =[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

    if (jsonObject != nil &&error == nil){

    return jsonObject;

    }else{

    // 解析错误

    return nil;

    }

    }


    获取到当前所在的视图

    + (UIViewController *)presentingVC{

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    if (window.windowLevel != UIWindowLevelNormal){

    NSArray *windows = [[UIApplication sharedApplication] windows];

    for(UIWindow * tmpWin in windows){

    if (tmpWin.windowLevel == UIWindowLevelNormal){

    window = tmpWin;

    break;

    }

    }

    }

    UIViewController *result = window.rootViewController;

    while (result.presentedViewController) {

    result = result.presentedViewController;

    }

    if ([result isKindOfClass:[UITabBarController class]]) {

    result = [(UITabBarController *)result selectedViewController];

    }

    if ([result isKindOfClass:[UINavigationController class]]) {

    result = [(UINavigationController *)result topViewController];

    }

    return result;

    }


    十六进制转UIColor

    + (UIColor*)colorWithHexString:(NSString*)stringToConvert{

    if([stringToConvert hasPrefix:@"#"])

    {

    stringToConvert = [stringToConvert substringFromIndex:1];

    }

    NSScanner*scanner = [NSScanner scannerWithString:stringToConvert];

    unsigned hexNum;

    if(![scanner scanHexInt:&hexNum])

    {

    return nil;

    }

    return[self colorWithRGBHex:hexNum];

    }

    + (UIColor*)colorWithRGBHex:(UInt32)hex{

    int r = (hex >>16) &0xFF;

    int g = (hex >>8) &0xFF;

    int b = (hex) &0xFF;

    return[UIColor colorWithRed:r /255.0f

    green:g /255.0f

    blue:b /255.0f

    alpha:1.0f];

    }


    获取当前时间的周一和周日的时间

    - (NSString *)getWeekTimeWithDate:(NSDate *)nowDate

    {

    //    NSDate *nowDate = [NSDate date];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];

    // 获取今天是周几

    NSInteger weekDay = [comp weekday];

    // 获取几天是几号

    NSInteger day = [comp day];

    //    NSLog(@"%ld----%ld",weekDay,day);

    // 计算当前日期和本周的星期一和星期天相差天数

    long firstDiff,lastDiff;

    //    weekDay = 1;

    if (weekDay == 1)

    {

    firstDiff = -6;

    lastDiff = 0;

    }

    else

    {

    firstDiff = [calendar firstWeekday] - weekDay + 1;

    lastDiff = 8 - weekDay;

    }

    //    NSLog(@"firstDiff: %ld  lastDiff: %ld",firstDiff,lastDiff);

    // 在当前日期(去掉时分秒)基础上加上差的天数

    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];

    [firstDayComp setDay:day + firstDiff];

    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];

    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];

    [lastDayComp setDay:day + lastDiff];

    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];

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

    [formatter setDateFormat:@"yyyy-MM-dd"];

    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];

    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];

    //    NSLog(@"%@=======%@",firstDay,lastDay);

    //周转换

    if (weekDay == 1) {

    weekDay = 7;

    }else{

    weekDay --;

    }

    NSString *dateStr = [NSString stringWithFormat:@"%@,%@,%zd",firstDay,lastDay,weekDay];

    return dateStr;

    }

    相关文章

      网友评论

          本文标题:iOS-常用工具方法

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