Xcode 11,迅捷.Dateformatter总是返回nil

编程入门 行业动态 更新时间:2024-10-25 15:25:17
本文介绍了Xcode 11,迅捷.Dateformatter总是返回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

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

发布评论

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

>www.elefans.com

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