美文网首页
iOS15.4.1 NSDateFormatter 返回为空问题

iOS15.4.1 NSDateFormatter 返回为空问题

作者: lesmiserables0 | 来源:发表于2022-05-12 17:02 被阅读0次

    今天解决了一个困扰我好几天的问题。
    解决起来很快,关键是复现问题,定位问题太难了。

    当手机系统时间为24小时制时,没有问题。当手机时间切换到12小时制时,格式化后的时间为nil。

        NSString *tempStr = @“2022-05-05 15:26:09”;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
       //  这里的yyyy 如果写成大写,就会在每年的年底,遭遇一年一次的跨年bug。
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *endDate = [formatter dateFromString:endTimeStr];
    

    这个bug与系统版本有关,应该属于兼容性问题。
    iOS12.5.2 没有发现这个问题。
    iOS14.7 没有发现这个问题。
    在iOS15.4.1系统下会有这个问题 。
    其他系统暂时没有条件测试。

    给NSDateFormatter 添加locale 属性可以解决这个问题:

        formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    

    在修改项目中的代码时,发现国外的三方库都添加了下面这行代码:


    截屏2022-05-12 17.34.03.png

    当然有些知名的三方库也存在这样的问题。


    截屏2022-05-12 17.34.32.png

    果然还是代码不规范的问题,唉😮💨。

    代码不规范,迟早吃大亏。

    相关文章

      网友评论

          本文标题:iOS15.4.1 NSDateFormatter 返回为空问题

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