Xcode 11,迅捷.Dateformatter总是返回nil [英] Xcode 11, swift. Dateformatter always return nil
问题描述
在Swift中将值从 String
转换为 Date
时遇到问题.
它总是返回nil.我在做什么错了?
让dateFormatter = DateFormatter()dateFormatter.dateFormat ="yyyy-MM-dd"let date = dateFormatter.date(来自:"2019-05-03")
这是Xcode调试器中发生的事情
更新:
这似乎是Xcode中的错误.在 viewDidLoad
中尝试代码并设置断点会导致日期的 lldb
描述为 nil
,但是可以正确打印出期望值
您可以在此
I have a problem with converting value from String
to Date
in Swift.
It always returns nil. What am I doing wrong?
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2019-05-03")
Here is what is happening in the debugger in Xcode
Update:
It would appear that it is a bug in Xcode. Trying your code in the viewDidLoad
and setting a breakpoint causes the lldb
description of the date to be nil
, however it correctly prints the expected value out.
You can see more about the bug at this SO post, thanks to OOPer for the link.
Currently this bug is still occurring in Xcode 11.2-beta
A couple of points
Use .dateFormat
instead of .format
Use the correct quotation marks "
instead of "
, also you should remove the space from your date format string.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2019-05-03")
这篇关于Xcode 11,迅捷.Dateformatter总是返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!