ios - NSDateFormatter - setLocale 被忽略了吗?
<p><p>我正在尝试解决一个臭名昭著的问题,即当用户将定位时间设置为 12 小时而不是默认的 24 小时时,NSDateFormatter 在不需要时返回“am/pm”日期(覆盖格式字符串)</p >
<p>阅读后,似乎最好的解决方案是将 dateFormatter 语言环境设置为 en_US_POSIX(我们将其用于 API 调用,因此需要忽略任何用户偏好)</p>
<p>但是,它似乎被忽略了?</p>
<p>我有一个分类如下:</p>
<pre><code>@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = ;
if (en_US_POSIX == nil) {
en_US_POSIX = [ initWithLocaleIdentifier:@"en_US_POSIX"];
}
;
return self;
}
@end
</code></pre>
<p>然后我打电话:</p>
<pre><code>sharedInstance.dateFormatter = [ initWithSafeLocale];
;
</code></pre>
<p>但是,日期仍然打印为</p>
<pre><code>2015-09-28 11:00:00 pm +0000
</code></pre>
<p>我做错了什么似乎忽略了 setLocale?</p>
<p>引用:<a href="https://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature" rel="noreferrer noopener nofollow">What is the best way to deal with the NSDateFormatter locale "feechur"?</a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题只是您如何使用 <code>NSLog()</code> 打印日期。这将调用 <code></code>,它将使用用户的默认语言环境。</p>
<p>来自 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/#//apple_ref/occ/instm/NSDate/descriptionWithLocale:" rel="noreferrer noopener nofollow">docs</a> (<code>descriptionWithLocale</code>):</p>
<blockquote>
<p>If you pass <code>nil</code>, <code>NSDate</code> formats the date in the same way as the
<code>description</code> property.</p>
<p>On OS X v10.4 and earlier, this parameter was an <code>NSDictionary</code> object.
If you pass in an <code>NSDictionary</code> object on OS X v10.5, <code>NSDate</code> uses the
default user locale—the same as if you passed in <code>[NSLocale
currentLocale]</code>.</p>
</blockquote>
<p>您应该使用日期格式化程序来打印格式化的字符串。</p></p>
<p style="font-size: 20px;">关于ios - NSDateFormatter - setLocale 被忽略了吗?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32737588/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32737588/
</a>
</p>
页:
[1]