我正在使用 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;
}
同样在文档中没有名为 local
的成员。
有什么方法可以直接使用 TimeZone
吗?或者我们必须使用 NSTimeZone
?
最佳答案
深入类层次,发现NSTimeZone
为public 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/