美文网首页iOS 开发 iOS Developer
iOS用NSDateFormatter解析日期Bug

iOS用NSDateFormatter解析日期Bug

作者: 小失 | 来源:发表于2016-05-30 17:02 被阅读629次

    下午刚发生一个bug, 用户生日日期是"1991-04-14", 在年龄显示那里, 本该直接展示年龄, 结果却直接将生日日期"1991-04-14"展现出来了.
    仔细看代码, 没发现什么问题:

    + (NSString *)ageFromBirthday:(NSString *)birthday
    {
        if (birthday == nil) return NSLocalizedString(@"is_not_set", nil);
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        
        NSDate *destDate = [dateFormatter dateFromString:birthday];
        if (destDate == nil) 
        {
            return birthday;
        }
        NSDate *now = [NSDate date];
        NSDateComponents* ageComponents = [[NSCalendar currentCalendar]
                                           components:NSCalendarUnitYear
                                           fromDate:destDate
                                           toDate:now
                                           options:0];
        NSInteger age = [ageComponents year];
        
        return [NSString stringWithFormat:@"%ld",age];
    
    }
    

    但是就是在日期是"1991-04-14"时, destDate = nil.
    然后将代码中birthday写死, 换成其他日期, 比如"1991-04-15" 之类的, 发现没问题.
    于是单纯的以为iOS系统在处理这个日期的时候出了bug, 还特意去网上搜了一下, 这天到底发生了什么事情, 是不是苹果公司那个工程师的特殊日子.
    没有什么结果. 倒是发现也有人恰好碰到这个灵异事件.

    于是单纯以为hardCode这个日期就行了啊! 然后同事发现了这个. 于是我觉得, 自己真的是太天真太想当然了. 应该搜索NSDateFormatter return nil啊! 果然SO上还是有解释.

    于是, 就这么干了:

    + (NSString *)ageFromBirthday:(NSString *)birthday
    {
        if (birthday == nil) return NSLocalizedString(@"is_not_set", nil);
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        
        NSDate *destDate = [dateFormatter dateFromString:birthday];
        
        NSDate *now = [NSDate date];
        if (destDate == nil) // 如果这样还是解析为空,且后台返回格式不是"yyyy-MM-DD",就返回默认值 18 吧
        {
            NSArray *subStrings = [birthday componentsSeparatedByString:@"-"];
            if (subStrings.count == 3)
            {
                // 拿到当前的年份
                NSCalendar *calendar = [NSCalendar currentCalendar];
                NSUInteger unitFlags = NSCalendarUnitYear;
                NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
                NSInteger year = [dateComponent year];
                NSInteger resultAge = year - [subStrings[0] integerValue];
                return [NSString stringWithFormat:@"%ld",resultAge];
            }
            return @"18";
        }
        NSDateComponents* ageComponents = [[NSCalendar currentCalendar]
                                           components:NSCalendarUnitYear
                                           fromDate:destDate
                                           toDate:now
                                           options:0];
        NSInteger age = [ageComponents year];
        
        return [NSString stringWithFormat:@"%ld",age];
    }
    

    期间因马虎大意写错好几次,也是蛋碎.

    相关文章

      网友评论

        本文标题:iOS用NSDateFormatter解析日期Bug

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