有没有办法检测应用程序的当前设备是否使用12h我们的24h格式,以便根据用户的语言/动作设置,我可以使用一个NSDateFormatter进行12h和一个进行24h的设置?就像UIDatePicker检测并显示AM/PM Select 器(如果是12h格式).

推荐答案

我想起来了,这很容易.我刚刚把这个代码添加到viewDidLoad:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatter release];
NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));

它完全返回YESNO,这取决于区域设置.

Objective-c相关问答推荐

CLLocationManager startUpdatingLocation 未调用 locationManager:didUpdateLocations: 或 locationManager:didFailWithError:

如何测试 NSCFBolean 值?

NSBundle pathForResource 为 NULL

如何在运行时向对象添加属性?

如何在 Cocoa 中创建字符串的 MD5 哈希?

在@implementation 而不是@interface 定义的类变量?

在 viewDidAppear 之前无法正确设置框架

如何在 iOS SDK 中使用语音识别?

Container View 好像有 UINavigationBar 一样被下推?

Objective-C 中的 const 与静态 NSStrings

更改 UIBarButtonItem 的 Tint colored颜色

在 ObjC 实现文件中声明的实例变量

以编程方式创建 UITableView

Objective-C - 何时使用touch

在 NSNumbers 的 NSArray 中查找最小值和最大值

ios 11 UITabBar UITabBarItem 定位问题

cellForRowAtIndexPath 是如何工作的?

Highlighted 和 Selected UIButton 的 State 有什么区别?

所有异常断点在模拟器上无缘无故停止

Xcode 警告未使用属性访问结果 - getter 不应用于副作用