前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS|一个与 NSDateFormatter 有关的小 Bug

iOS|一个与 NSDateFormatter 有关的小 Bug

作者头像
mzlogin
发布2024-07-06 14:08:06
740
发布2024-07-06 14:08:06
举报
文章被收录于专栏:闷骚的程序员闷骚的程序员

我们的 iOS APP 有一个小 Bug,场景简化后是这样:

接口返回一个时间字符串,APP 里比较它与当前时间,如果当前时间晚于它,就显示一个按钮,否则不显示。

本来是一个很简单的逻辑,但是,有一部分用户反馈,按钮该显示的时候却没有显示。

分析

结合用户反馈的信息,经过多次尝试后,才发现这个行为竟然与用户手机的时间制式有关——如果用户手机设置里的 24小时制 开关没有打开,那么这个 Bug 就会出现。

相关的逻辑是这样写的:

代码语言:javascript
复制
NSDate *remoteDate = [NSDate dateFromStr:remoteDateString];
if (remoteDate) {
    // 比较 remoteDate 和 本地当前时间,控制按钮显隐
}

这个 dateFromStr: 是一个 category 方法,实现是这样的:

代码语言:javascript
复制
+ (NSDate*)dateFromStr:(NSString *)dateStr {
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter dateFromString:dateStr];
}

经过调试,发现 remoteDate24小时制 开关关闭时,返回的是 nil,而在打开时,返回的是正确的时间。

苹果官方文档里,NSDateFormatterdateFromString: 方法是这样描述的:

Returns a date representation of a given string interpreted using the receiver’s current settings. Return Value A date representation of string. If dateFromString: can’t parse the string, returns nil.

同时还给出了 Working With Fixed Format Date Representations 的参考链接,里面有说明:

When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string. For most fixed formats, you should also set the locale property to a POSIX locale (“en_US_POSIX”), and set the timeZone property to UTC.

这个页面里还给出了一个 QA 链接 Technical Q&A QA1480 “NSDateFormatter and Internet Dates”,里面有这样的描述:

On iOS, the user can override the default AM/PM versus 24-hour time setting (via Settings > General > Date & Time > 24-Hour Time), which causes NSDateFormatter to rewrite the format string you set, which can cause your time parsing to fail. … On the other hand, if you’re working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format.

里面提到了用户可以通过设置 24小时制 来影响 NSDateFormatter 的行为,还提到了当尝试把固定格式的日期字符串转换成日期对象时,应该设置 locale

至此破案了,这个 Bug 就是由于没有设置 NSDateFormatterlocale 属性导致的。

解决

修改后的代码是这样的,仅加了一行 locale 设置:

代码语言:javascript
复制
+ (NSDate*)dateFromStr:(NSString *)dateStr {
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    return [dateFormatter dateFromString:dateStr];
}

经过测试功能正常了,不管用户手机的 24小时制 开关是否打开,都能正常解析服务端返回的时间字符串了。

参考

文档信息

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024/07/05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 分析
  • 解决
  • 参考
    • 文档信息
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档