- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
NSDateFormatter
似乎有一个“特性”让你意想不到:如果你做一个简单的“固定”格式操作比如:
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyyMMddHHmmss"];
NSString* dateStr = [fmt stringFromDate:someDate];
[fmt release];
然后它在美国和大多数地区都可以正常工作直到......有人将他们的手机设置为 24 小时区域将设置中的 12/24 小时开关设置为 12。然后上面开始添加“AM”或“PM"到结果字符串的末尾。
(参见,例如,NSDateFormatter, am I doing something wrong or is this a bug?)
(参见 https://developer.apple.com/library/content/qa/qa1480/_index.html)
显然 Apple 已经宣布这是“BAD”——按设计损坏,他们不会修复它。
规避显然是为特定地区(通常是美国)设置日期格式化程序的语言环境,但这有点困惑:
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setLocale: loc];
[loc release];
在 onesies-twosies 中还不错,但我正在处理大约 10 个不同的应用程序,我看到的第一个应用程序有 43 个这种情况的实例。
那么对于宏/重写类/任何东西有什么聪明的想法可以最大限度地减少更改所有内容的工作量,而不会使代码变得晦涩难懂吗? (我的第一直觉是用一个可以在 init 方法中设置语言环境的版本覆盖 NSDateFormatter。需要更改两行——alloc/init 行和添加的导入。)
##已添加
这是我到目前为止的想法——似乎适用于所有情况:
@implementation BNSDateFormatter
-(id)init {
static NSLocale* en_US_POSIX = nil;
NSDateFormatter* me = [super init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
[me setLocale:en_US_POSIX];
return me;
}
@end
##更新关于 OMZ 的提案,这是我的发现 --
这里是分类版本--h文件:
#import <Foundation/Foundation.h>
@interface NSDateFormatter (Locale)
- (id)initWithSafeLocale;
@end
m类文件:
#import "NSDateFormatter+Locale.h"
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = [super init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
NSLog(@"Category's locale: %@ %@", en_US_POSIX.description, [en_US_POSIX localeIdentifier]);
[self setLocale:en_US_POSIX];
return self;
}
@end
代码:
NSDateFormatter* fmt;
NSString* dateString;
NSDate* date1;
NSDate* date2;
NSDate* date3;
NSDate* date4;
fmt = [[NSDateFormatter alloc] initWithSafeLocale];
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
dateString = [fmt stringFromDate:[NSDate date]];
NSLog(@"dateString = %@", dateString);
date1 = [fmt dateFromString:@"2001-05-05 12:34:56"];
NSLog(@"date1 = %@", date1.description);
date2 = [fmt dateFromString:@"2001-05-05 22:34:56"];
NSLog(@"date2 = %@", date2.description);
date3 = [fmt dateFromString:@"2001-05-05 12:34:56PM"];
NSLog(@"date3 = %@", date3.description);
date4 = [fmt dateFromString:@"2001-05-05 12:34:56 PM"];
NSLog(@"date4 = %@", date4.description);
[fmt release];
fmt = [[BNSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
dateString = [fmt stringFromDate:[NSDate date]];
NSLog(@"dateString = %@", dateString);
date1 = [fmt dateFromString:@"2001-05-05 12:34:56"];
NSLog(@"date1 = %@", date1.description);
date2 = [fmt dateFromString:@"2001-05-05 22:34:56"];
NSLog(@"date2 = %@", date2.description);
date3 = [fmt dateFromString:@"2001-05-05 12:34:56PM"];
NSLog(@"date3 = %@", date3.description);
date4 = [fmt dateFromString:@"2001-05-05 12:34:56 PM"];
NSLog(@"date4 = %@", date4.description);
[fmt release];
结果:
2011-07-11 17:44:43.243 DemoApp[160:307] Category's locale: <__NSCFLocale: 0x11a820> en_US_POSIX
2011-07-11 17:44:43.257 DemoApp[160:307] dateString = 2011-07-11 05:44:43 PM
2011-07-11 17:44:43.264 DemoApp[160:307] date1 = (null)
2011-07-11 17:44:43.272 DemoApp[160:307] date2 = (null)
2011-07-11 17:44:43.280 DemoApp[160:307] date3 = (null)
2011-07-11 17:44:43.298 DemoApp[160:307] date4 = 2001-05-05 05:34:56 PM +0000
2011-07-11 17:44:43.311 DemoApp[160:307] Extended class's locale: <__NSCFLocale: 0x11a820> en_US_POSIX
2011-07-11 17:44:43.336 DemoApp[160:307] dateString = 2011-07-11 17:44:43
2011-07-11 17:44:43.352 DemoApp[160:307] date1 = 2001-05-05 05:34:56 PM +0000
2011-07-11 17:44:43.369 DemoApp[160:307] date2 = 2001-05-06 03:34:56 AM +0000
2011-07-11 17:44:43.380 DemoApp[160:307] date3 = (null)
2011-07-11 17:44:43.392 DemoApp[160:307] date4 = (null)
手机 [make that an iPod Touch] 设置为 Great Britain,12/24 开关设置为 12。两个结果有明显差异,我判断类别版本有误。请注意,类别版本中的日志正在执行(并且命中代码中的停止),因此这不仅仅是代码以某种方式未被使用的情况。
##一个奇怪的观察
稍微修改了类别实现:
#import "NSDateFormatter+Locale.h"
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX2 = nil;
self = [super init];
if (en_US_POSIX2 == nil) {
en_US_POSIX2 = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
NSLog(@"Category's locale: %@ %@", en_US_POSIX2.description, [en_US_POSIX2 localeIdentifier]);
[self setLocale:en_US_POSIX2];
NSLog(@"Category's object: %@ and object's locale: %@ %@", self.description, self.locale.description, [self.locale localeIdentifier]);
return self;
}
@end
基本上只是更改了静态语言环境变量的名称(以防与子类中声明的静态变量发生冲突)并添加了额外的 NSLog。但是看看 NSLog 打印的内容:
2011-07-15 16:35:24.322 DemoApp[214:307] Category's locale: <__NSCFLocale: 0x160550> en_US_POSIX
2011-07-15 16:35:24.338 DemoApp[214:307] Category's object: <NSDateFormatter: 0x160d90> and object's locale: <__NSCFLocale: 0x12be70> en_GB
2011-07-15 16:35:24.345 DemoApp[214:307] dateString = 2011-07-15 04:35:24 PM
2011-07-15 16:35:24.370 DemoApp[214:307] date1 = (null)
2011-07-15 16:35:24.378 DemoApp[214:307] date2 = (null)
2011-07-15 16:35:24.390 DemoApp[214:307] date3 = (null)
2011-07-15 16:35:24.404 DemoApp[214:307] date4 = 2001-05-05 05:34:56 PM +0000
如您所见,setLocale 根本没有。格式化程序的语言环境仍然是 en_GB。似乎类别中的 init 方法有些“奇怪”。
最佳答案
有时您会“啊哈!!”片刻,有时更像是“Duh !!”这是后者。在 initWithSafeLocale
的类别中,“super”init
被编码为 self = [super init];
。这会初始化 NSDateFormatter
的父类(super class),但不会初始化
NSDateFormatter
对象本身。
显然,当此初始化被跳过时,setLocale
“反弹”,大概是因为对象中缺少某些数据结构。将 init
更改为 self = [self init];
会导致 NSDateFormatter
初始化发生,并且 setLocale
很高兴再次。
这是该类别的 .m 的“最终”来源:
#import "NSDateFormatter+Locale.h"
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = [self init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
[self setLocale:en_US_POSIX];
return self;
}
@end
关于ios - 处理 NSDateFormatter 语言环境 "feature"的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260373/
好的,所以我刚刚开始尝试将 BDD 用于我们正在进行的一些新开发,并且我为日志查看器功能写了一个这样的故事: 故事:用户查看工作流执行日志 As a user I want to review the
我正在尝试使用 TensorFlow 编写一个简单的深度机器学习模型。我正在使用我在 Excel 中制作的玩具数据集,只是为了让模型工作并接受数据。我的代码如下: import pandas as p
我是机器学习的初学者。我很困惑如何将数据集的不同特征组合成一个特征。 例如,我在 Python Pandas 数据框架中有一个数据集,其特征如下: movie unknown actio
我正在做一项功能调查,为一个研究项目做准备。 说出难以优化的主流语言或语言功能,以及为什么该功能值得或不值得付出代价,或者只是用轶事证据驳斥我下面的理论。在有人将其标记为主观之前,我要求提供语言或功能
这是一个有点哲学问题。我正在为我的软件添加一个小功能,我认为大多数用户都会使用它,但他们使用该软件的次数可能只有 10%。换句话说,该软件没有它 3 个月就很好,但是有 4 或 5 个用户要求它,我同
我开始使用 git flow。我创建了一个功能: git flow feature start eval 然后我做了一些工作并添加并提交了更改: git add (files) git commit
pull 请求是内置在 Git 中还是 GitHub 虚构的概念? 最佳答案 概念和该概念的实现之间存在区别。 “请求 pull ”的概念是 DVCS 系统有别于传统版本控制系统的部分原因。使用传统的
研究该主题,可以找到作者使用“词袋”模型进行图像分类/检索的论文,而其他人则使用“特征袋”模型进行类似任务。 尽管我对所涉及的方法有基本的了解(检测和提取视觉词、构建视觉词典、使用机器学习训练分类器)
有时一首歌会有不止一个艺术家。例如,Jay-z 的新歌“A Star is Born”以艺术家 Cole 为主角,因此在目录中会被列为“Jay-z(以 Cole 为主角)- A Star is Bor
This question already has an answer here: How do I 'pass down' feature flags to subdependencies in C
This question and answer演示当使用 scikit-learn 的专用特征选择例程之一执行特征选择时,可以按如下方式检索所选特征的名称: np.asarray(vectorize
例如,我定义了 2 个没有依赖关系的特性: [features] default = [] py2 = [] py3 = [] 基于选定的功能 (--features py3) 我想为依赖项 (cpy
我正在完成一个小型 Wordpress“杂志”类型网站的定制。由于我是 PHP 的新手,我遇到了一些需要帮助的问题。 我有一个“首屏,主要特色区域,包含 3 张图片”和帖子标题的小摘录。在首屏下,我在
我已经为 Windows 10 创建了一个 C# 应用程序。它是通过使用 WIX 生成的 MSI 安装的。但是,当它为一台机器上的一个用户安装时,并非出于我的意图,它不会为同一台机器上的其他用户安装。
在 ArcGIS Runtime Java API 文档中,有一个 identifyLayersAsync() method . 来自文档: Asynchronously identifies the
我是 GIT 和 GIT-Flow 的新手。 [在我的 python-django 项目上] 我做了什么: git flow feature start new_feature # perform s
我是 Angular 的新手,我正在尝试使用 Angular/d3 构建德国 map 。 map 数据存储在 Topojson 文件 plz_map_ger.json 中: { "type": "To
我一直在使用 503 服务不可用 或停机维护。 但是一些 http 客户端库,即 axios 将 503 视为可重试错误。 如果由于高负载而产生响应,则重试它是有意义的,但 503 也适合功能切换情况
要列出您希望包含在生成的 features.xml 中的一堆包,文档说: bundles File A properties file that contains a list of bund
我在 Visual Studio 2010 下开发 C# T4 预处理模板时遇到以下编译错误: A template containing a class feature must end with
我是一名优秀的程序员,十分优秀!