objective-c - 在 Swift 中创建 NSDateFormatter?

标签 objective-c swift date nsdate nsdateformatter

<分区>

我是 swift 的新手,对 Objective-C 非常陌生。有人可以帮我把它转换成 Swift 吗?我从 Ray Wenderlich 的最佳 iOS 实践中得到这段代码 - http://www.raywenderlich.com/31166/25-ios-app-performance-tips-tricks

你会把这段代码放在哪里?它会放在一个充满全局变量的类文件中吗?

- (NSDateFormatter *)formatter {
    static NSDateFormatter *formatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; // twitter date format
    });
    return formatter;
}

最佳答案

  1. 如果您真的在寻找问题中方法的直接模拟,您可以这样做:

    class MyObject {
    
        // define static variable
    
        private static let formatter: DateFormatter = {
            let formatter = DateFormatter()
            formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
            return formatter
        }()
    
        // you could use it like so
    
        func someMethod(date: Date) -> String {
            return MyObject.formatter.string(from: date)
        }
    }
    

    static 属性,像全局变量一样,享有 dispatch_once 的默认值行为。有关详细信息,请参阅 Files and Initialization 末尾的 dispatch_once 讨论。 Apple 的 Swift 博客中的条目。

  2. 关于日期格式化程序的最佳实践,我建议:

    • 是的,明智的做法是不要不必要地创建和销毁您可能再次需要的格式化程序。在 WWDC 2012 视频中,iOS App Performance: Responsiveness , Apple 明确鼓励我们

      • 为每种日期格式缓存一个格式化程序;

      • 通过 NotificationCenterNSLocale.currentLocaleDidChangeNotification 添加观察者,如果发生这种情况,则清除/重置缓存的格式化程序;和

      • 请注意,重置格式与重新创建一样昂贵,因此请避免重复更改格式化程序的格式字符串。

      最重要的是,如果您重复使用相同的日期格式,请尽可能重复使用日期格式化程序。

    • 如果使用 DateFormatter 解析要与 Web 服务交换(或存储在数据库中)的日期字符串,您应该使用 en_US_POSIX 语言环境以避免出现问题与可能不使用公历的国际用户一起使用。参见 Apple Technical Q&A #1480DateFormatter documentation 中的“使用固定格式日期表示”讨论. (或者在 iOS 10 和 macOS 10.12 中,使用 ISO8601DateFormatter 。)

      但是当使用 dateFormatDateFormatter 时,使用当前区域设置来创建要呈现给最终用户的字符串,但是使用 en_US_POSIX 本地创建/解析要在应用内部使用或与 Web 服务交换的字符串时。

    • 如果为用户界面格式化日期字符串,请尽可能避免对 dateFormat 使用字符串文字来本地化字符串。尽可能使用 dateStyletimeStyle。如果您必须使用自定义 dateFormat,请使用模板来本地化您的字符串。例如,在 Swift 3 和更高版本中,您可以使用 dateFormat(fromTemplate:options:locale:):

      而不是硬编码 E, MMM d
      formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "EdMMM", options: 0, locale: Locale.current)
      

      例如,这会自动为美国用户显示“9 月 5 日星期一”,为英国用户显示“9 月 5 日星期一”。

    • DateFormatter 现在在 iOS 7 及更高版本以及在 macOS 10.9 及更高版本上运行的 64 位应用程序上是线程安全的。

有关 Swift 2 示例,请参阅 previous revision of this answer .

关于objective-c - 在 Swift 中创建 NSDateFormatter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28504589/

相关文章:

ios - sendSubviewToBack 和 bringSubviewToFront 导致 Root View Controller 的 subview 向上移动并被导航栏重叠

ios - UIDatePicker 向用户显示今天的日期为 "Today"(Swift)

iphone - 在没有 UInavigationController 的情况下将 UIBarButtonItem 添加到 UINavigation 栏

objective-c - 通过 Game Center 发送 NSString

ios - AFNetworking 和后台传输

inheritance - swift 从其基类返回子类

swift - 使用 CryptoTokenKit 读取 NFC (Mifare Classic 1K)

javascript - 按文本对多维数组进行排序

date - 宏以非标准格式在当前日期的文件夹中选择文件

java - 转换为 JSON 字符串时日期递减一天 - Java