NSDateFormatter 在 Swift 3 中可用吗?

Is NSDateFormatter available in Swift 3?

我尝试在使用 DatePicker 后在我的代码中使用它,但是没有针对 NSDateFormatter 的建议,只有 DateFormatter 出现了。 如果有人在他们的代码中使用过,请在这里分享。 另外,我应该做哪些更改才能收到建议。

我想将格式从 MM-DD-YYYY 更改为 DD-MM-YYYY。

试试这个

let dateFormatter = DateFormatter()
        //format in which recived Date Present
        dateFormatter.dateFormat = "dd-MMM-yyyy"
        let newDate = dateFormatter.date(from: "20-MAR-2017")
        // format to which you need your date to convert
        dateFormatter.dateFormat = "MMM-dd-yyyy"
        if let newDate  = newDate{
            let dateStr = dateFormatter.string(from: newDate) 

            print(dateStr) //Output: MAR-20-2017

        }

Swift 3 已删除 NS 前缀,因此您不会获得 NSDateFormatter 的建议。 DateFormatterNSDateFormatter 相同,只是名称不同。您可以像使用 NSDateFormatter 一样使用 DateFormatter。查看 SE-0086 提案了解更多详情。