ios - 类型 'TimeZone' 在 Swift3 中没有成员 'local'

标签 ios swift swift3 nstimezone

我正在使用 Xcode8 Beta4 将 Swift2.3 开发的项目转换为 Swift3.0。其中我有将日期转换为字符串的方法,但无法转换。

class func convertDateToString(_ date:Date, dateFormat:String) -> String?
{
    let formatter:DateFormatter = DateFormatter();
    formatter.dateFormat = dateFormat;
    formatter.timeZone = TimeZone.local
    let str = formatter.string(from: date);
    return str;
}

enter image description here

同样在文档中没有名为 local 的成员。

有什么方法可以直接使用 TimeZone 吗?或者我们必须使用 NSTimeZone

最佳答案

深入类层次,发现NSTimeZonepublic typealias,为我们打开了NSTimeZone的访问权限。

TimeZone

public struct TimeZone : CustomStringConvertible, CustomDebugStringConvertible, Hashable, Equatable, ReferenceConvertible {

    public typealias ReferenceType = NSTimeZone
}

所以通过使用下面的语法错误就消失了。

所以下面的代码可以工作。

对于本地时区。

formatter.timeZone = TimeZone.ReferenceType.local

对于默认时区。 使用具有相同语法的 default

formatter.timeZone =  TimeZone.ReferenceType.default

对于系统时区。 使用具有相同语法的 system

formatter.timeZone = TimeZone.ReferenceType.system

swift 3

您可以使用 .current 代替 .local

TimeZone.current

关于ios - 类型 'TimeZone' 在 Swift3 中没有成员 'local',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39341941/

相关文章:

ios - 禁用 UIAlertController 框架外的触摸以取消

java - 是否有低级 Swift 类可以在 AWS S3 中上传文件

ios - 在 CollectionView 中滚动 tableView 时隐藏 NavigationBar?

json - 从 TableView 中的 JSON 数据下载可重复使用的单元格或错误的异步图像

ios - AVAudioSession 从未停止

ios - 类方法访问级别默认为 internal

ios - 重命名/重构 View Controller

ios - ENABLE_BITCODE 问题。 Xcode 7 归档问题

swift - 为什么 "Show detail"segue 在我的 splitview 中不可用?

ios - Xcode 8 Storyboard问题