NSDateFormatter在swift和iOS SDK 8.0中返回nil

编程入门 行业动态 更新时间:2024-10-07 06:49:20
本文介绍了NSDateFormatter在swift和iOS SDK 8.0中返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读Raywenderlich提供的教程,第29章新内容测试,并遇到一个奇怪的问题。

以下是教程中将字符串转换为日期的代码:

//将日期字符串转换为日期。 let dateFormatter = NSDateFormatter() dateFormatter.dateFormat =yyyy-MM-dd HH:mm:ss zzz var date:NSDate? = dateFormatter.dateFromString(dateString)

dateString的格式为

2014-06-21 14:56:00 EST

但是,日期变量总是为零。

注意:在操场上播放此代码时,它可以正常工作,如图所示: / p>

我在iOS SDK 8.0中工作。这是SDK的错误吗?

更新

我正在使用iOS 8的最新iPod Touch进行测试。

解决方案

当您设置 dateFormat string您还必须将 locale 属性设置为与提供的格式兼容的属性。否则,区域设置将基于可能不兼容的设备设置。

此处提供的日期格式将与en区域设置一起使用但不起作用与许多其他人,如欧盟。这是一个例子:

let dateString =2014-06-21 14:56:00 EST 让localeStr =eu//这将给出nil 让localeStr =us//这将成功 let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier:localeStr) dateFormatter.dateFormat =yyyy-MM-dd HH:mm:ss zzz var date:NSDate? = dateFormatter.dateFromString(dateString)

这里的问题是EST,它只存在于北美洲。在所有其他语言环境中,它是无效的时区。如果您将日期字符串更改为:

2014-06-21 14:56:00 UTC-5

然后无论什么值 locale 设置为。

I am reading the tutorial provided by Raywenderlich, Chapter 29 What’s New with Testing, and run into a strange problem.

Following is the code in the tutorial converting a string into date:

// Convert date string to date. let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz" var date: NSDate? = dateFormatter.dateFromString(dateString)

The dateString is of the form

2014-06-21 14:56:00 EST

However, the date variable is always nil.

NOTE: when playing this code in the playground, it works properly, as the image shows:

I am working in iOS SDK 8.0. Is it a bug of the SDK?

Updated

I am testing using a latest iPod Touch with iOS 8.

解决方案

When you set a dateFormat string you must also set the locale property to something that is compatible with the format provided. Otherwise the locale will be based on the device settings which may not be compatible.

The date format you provided here will work with the "en" locale but it will not work with many others, such as "eu". Here's an example:

let dateString = "2014-06-21 14:56:00 EST" let localeStr = "eu" // this will give nil let localeStr = "us" // this will succeed let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: localeStr) dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz" var date: NSDate? = dateFormatter.dateFromString(dateString)

The problem here is "EST" which only exists in the north america. In all other locales it is an invalid timezone. If you change your date string to this:

"2014-06-21 14:56:00 UTC-5"

Then it the date will correctly format no matter what value locale is set to.

更多推荐

NSDateFormatter在swift和iOS SDK 8.0中返回nil

本文发布于:2023-11-26 23:32:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:iOS   swift   NSDateFormatter   nil   SDK

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!