Ios 类型';时区';没有成员';本地';迅捷3
我正在使用Xcode8 Beta4将Swift2.3中开发的项目转换为Swift3.0。其中我有一个方法将日期转换为字符串,但它无法转换它Ios 类型';时区';没有成员';本地';迅捷3,ios,swift,swift3,nstimezone,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; formatt
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.current
有没有办法直接使用时区?或者我们必须使用
NSTimeZone
?通过深入类层次结构,找到NSTimeZone
作为public typealias
,这为我们打开了对NSTimeZone
的访问
时区内
public struct TimeZone : CustomStringConvertible, CustomDebugStringConvertible, Hashable, Equatable, ReferenceConvertible {
public typealias ReferenceType = NSTimeZone
}
所以通过使用下面的语法,错误就会消失
所以下面的代码可以工作
对于本地时区
formatter.timeZone = TimeZone.ReferenceType.local
对于默认时区。
使用相同语法的default
formatter.timeZone = TimeZone.ReferenceType.default
formatter.timeZone = TimeZone.ReferenceType.system
对于系统时区。
使用相同语法的system
formatter.timeZone = TimeZone.ReferenceType.default
formatter.timeZone = TimeZone.ReferenceType.system
Swift 3
您可以使用.current
而不是.local
TimeZone.current
在swift 3中使用此选项而不是.local
:
TimeZone.current
尝试此格式化程序。时区=.localNSTimeZone.local
有效,与.system
和相同。默认值
。你可以在Apple@Anbu.Karthik:.local
上提交一份错误报告。@technerd-我在谷歌上搜索过,我没有得到与此相关的数据,martin评论也可以吗follow@Anbu.Karthik:试图找到解决方案,如果没有,则将提交错误。顺便说一句,谢谢Kartik。只需使用NSTimeZone.local
或local,NSTimeZone.default
作为默认值,而NSTimeZone.system
作为默认值system@LeoDabus:是的,我们可以直接使用NSTimeZone
,但当Xcode自动将Swift2.3代码转换为Swift3.0时,它会将NSTimeZone
转换为“时区”。因此建议使用Swift类。所以,如果任何人想使用时区,那么它就会引起问题。谢谢