【问题标题】:Type 'TimeZone' has no member 'local' in Swift3类型“时区”在 Swift3 中没有成员“本地”
【发布时间】:2016-09-06 06:27:33
【问题描述】:

我正在使用 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

【问题讨论】:

  • 试试这个 formatter.timeZone =.local
  • NSTimeZone.local 有效,.system.default 相同。您可以向 Apple 提交错误报告。
  • @Anbu.Karthik : .local 也不起作用。
  • @technerd - 我在谷歌搜索我没有得到相关的文件,可能马丁的评论也可以关注
  • @Anbu.Karthik :试图找到解决方案,如果没有,那么将提交错误。顺便感谢 Kartik。

标签: ios swift swift3 nstimezone


【解决方案1】:

通过深入类层次,发现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

斯威夫特 3

您可以使用.current 代替.local

TimeZone.current

【讨论】:

  • 只使用NSTimeZone.local tor Local,NSTimeZone.default 用于默认,NSTimeZone.system 用于系统
  • @LeoDabus:是的,我们可以直接使用NSTimeZone,但是当Xcode自动将Swift2.3代码转换为Swift3.0时,它会将NSTimeZone转换为'TimeZone'。所以推荐使用 Swift 类。所以如果有人想使用TimeZone,那么它会引起问题。谢谢!
【解决方案2】:

在 swift 3 中使用这个代替 .local

TimeZone.current

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多