iOS系统区域设置标识符为空
我在使用日期格式化程序时注意到一些奇怪的事情。下面是日期格式化程序的代码iOS系统区域设置标识符为空,ios,swift,nsdate,nsdateformatter,Ios,Swift,Nsdate,Nsdateformatter,我在使用日期格式化程序时注意到一些奇怪的事情。下面是日期格式化程序的代码 let formatter = NSDateFormatter() formatter.dateFormat = "EEE dd MMM h:mm a" formatter.locale = NSLocale.systemLocale() print(formatter.stringFromDate(NSDate()) ) 输出为:“Fri 18 M03 1:05 PM”。这有点奇怪。但是,删除格式化程序的区域设置会得到
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE dd MMM h:mm a"
formatter.locale = NSLocale.systemLocale()
print(formatter.stringFromDate(NSDate()) )
输出为:“Fri 18 M03 1:05 PM”。这有点奇怪。但是,删除格式化程序的区域设置会得到我想要的输出:“Fri 18 Mar 1:05 PM”
我还尝试打印出NSLocale.systemLocale(),输出为空字符串。这正常吗?当您更改区域设置时,日期格式化程序实际上发生了什么
仅供参考:我正在实际设备上测试。此外,更改设备设置中的区域格式对区域设置标识符没有影响。如果要使用系统设置,应使用此设置
NSLocale.currentLocale()
在苹果的API文档中已经有一个观点
讨论
如果不需要任何本地化,请使用系统区域设置。使用当前区域设置设置显示给用户的文本格式
供参考: