【问题标题】:How to convert this Swift syntax into Objective-C?如何将此 Swift 语法转换为 Objective-C?
【发布时间】:2016-04-16 17:20:34
【问题描述】:

我正在将 Swift 库转换为 Objective-C 作为练习。

如何将其转换为 Objective-C ?

let formatter = NSDate.formatter(format: dateFormat)

我试过了:

NSDateFormatter *formatter = [NSDate formatterWithFormat : dateFormat];

也试过这个:

NSDateFormatter *formatter = [NSDate formatterWithFormat : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];

也试过这个:

NSDateFormatter *formatter = [NSDate formatter : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];

错误:不知道选择器“formatterWithFormat:”或“formatter :::”的类方法

更多上下文:

+ (NSDateFormatter *) formatter : (NSString *) format  : (NSTimeZone*) timeZone  : (NSLocale *) locale {
    format = DefaultFormat;
    timeZone = [NSTimeZone localTimeZone];
    locale = [NSLocale currentLocale];
    NSString *hashKey = [NSString stringWithFormat:@"%lu%lu%lu", (unsigned long)format.hash, (unsigned long)timeZone.hash, (unsigned long)locale.hash];
    NSMutableDictionary *formatters = [NSDate sharedDateFormatters];
    NSDateFormatter *cachedDateFormatter = formatters[hashKey];
    if (cachedDateFormatter != nil) {
        return cachedDateFormatter;
    }
    else {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = format;
        formatter.timeZone = timeZone;
        formatter.locale = locale;
        formatters[hashKey] = formatter;
        return formatter;
    }
}

中间有一些随机文本,所以我可以添加更多代码...

+ (NSDateFormatter *)formatter:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle relativeDateFormatting:(BOOL)doesRelativeDateFormatting timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
    timeZone = [NSTimeZone localTimeZone];
    locale = [NSLocale currentLocale];
    NSString *hashKey = [NSString stringWithFormat:@"%lu%lu%lu%lu%lu", (unsigned long)dateStyle, (unsigned long)timeStyle, (unsigned long)doesRelativeDateFormatting, (unsigned long)timeZone.hash, (unsigned long)locale.hash];
    NSMutableDictionary *formatters = [NSDate sharedDateFormatters];
    NSDateFormatter *cachedDateFormatter = formatters[hashKey];
    if (cachedDateFormatter != nil) {
        return cachedDateFormatter;
    }
    else {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateStyle = dateStyle;
        formatter.timeStyle = timeStyle;
        formatter.doesRelativeDateFormatting = doesRelativeDateFormatting;
        formatter.timeZone = timeZone;
        formatter.locale = locale;
        formatters[hashKey] = formatter;
        return formatter;
    }
}

原始 Swift 代码:

private class func formatter(format format:String = DefaultFormat, timeZone: NSTimeZone = NSTimeZone.localTimeZone(), locale: NSLocale = NSLocale.currentLocale()) -> NSDateFormatter {
            let hashKey = "\(format.hashValue)\(timeZone.hashValue)\(locale.hashValue)"
            var formatters = NSDate.sharedDateFormatters()
            if let cachedDateFormatter = formatters[hashKey] {
                return cachedDateFormatter
            } else {
                let formatter = NSDateFormatter()
                formatter.dateFormat = format
                formatter.timeZone = timeZone
                formatter.locale = locale
                formatters[hashKey] = formatter
                return formatter
            }
        }

中间有一些随机文本,所以我可以添加更多代码...

private class func formatter(dateStyle dateStyle: NSDateFormatterStyle, timeStyle: NSDateFormatterStyle, doesRelativeDateFormatting: Bool, timeZone: NSTimeZone = NSTimeZone.localTimeZone(), locale: NSLocale = NSLocale.currentLocale()) -> NSDateFormatter {
        var formatters = NSDate.sharedDateFormatters()
        let hashKey = "\(dateStyle.hashValue)\(timeStyle.hashValue)\(doesRelativeDateFormatting.hashValue)\(timeZone.hashValue)\(locale.hashValue)"
        if let cachedDateFormatter = formatters[hashKey] {
            return cachedDateFormatter
        } else {
            let formatter = NSDateFormatter()
            formatter.dateStyle = dateStyle
            formatter.timeStyle = timeStyle
            formatter.doesRelativeDateFormatting = doesRelativeDateFormatting
            formatter.timeZone = timeZone
            formatter.locale = locale
            formatters[hashKey] = formatter
            return formatter
        }
    }

【问题讨论】:

  • 如果这是您尝试调用的方法,那么您需要传递所有 3 个参数,而不仅仅是一个。
  • 如果您只是覆盖它们的值,那么传入这三个参数有什么意义呢?您没有使用我在上一个问题中给您的代码。
  • 实际上,我做到了。让我将此添加到问题中。请记住,我是从现有的 Swift 代码转换它并尽量保持相似。
  • 我删除了设置timeZonelocale 的前两行。这些行毫无意义,因为您提供了所需的值作为参数。
  • 仍然会出现错误,因为您只传递了一个参数而不是所有必需的参数。

标签: objective-c swift


【解决方案1】:

您的语法转换在语法上是正确的。

但是,NSDate 不提供名为formatterWithFormat:formatter 的静态方法;您正在翻译的代码似乎有一个 extension 方法。你需要找到那个方法,然后翻译它。

注意:我强烈建议反对将其作为NSDate 的扩展,因为它属于类层次结构的NSDateFormatter 一侧。

【讨论】:

  • 我同意这个说明。在NSDate 类上使用这样的方法是没有意义的。
  • 我想我需要提供更多的上下文。我将编辑我的问题。
【解决方案2】:

根据您的所有更新,您的 Objective-C 代码和 Swift 代码之间的主要区别在于 Swift 函数为参数提供了默认值。这允许您使用零个或多个参数调用函数(在这种情况下)。

由于 Objective-C 不支持默认参数值,因此您需要为每个参数传递一个值。

方法:

+ (NSDateFormatter *) formatter : (NSString *) format  : (NSTimeZone*) timeZone  : (NSLocale *) locale {
}

需要调用为:

[WhateverClassThisIs formatter:someFormat :someTimeZone :someLocale];

请注意由于您没有命名所有参数而导致的丑陋语法(就像我在上一个问题中展示的那样。

您还需要删除添加的行,以便在方法开始时为这些参数赋值。

【讨论】:

  • 好的,我试过这个: NSDateFormatter *formatter = [NSDate formatterWithFormat : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];但我仍然遇到同样的错误。
  • 顺便说一句(请将此视为有益的、建设性的评论)-鉴于您在过去几天提出的所有问题,我强烈建议您找到一个很好的教程Objective-C 编程语言。现在花时间学习语法将为您节省大量时间。
  • 哈哈哈!实际上,我专门将 Swift 库转换为 Objective-C 来学习语法(在一些教程和整个课程之后)。但是谢谢你的评论!
  • 顺便说一句:仍然不起作用。 NSDateFormatter *formatter = [NSDate formatter : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]];
  • 你把这个+formatter:::方法加到哪个类了?
猜你喜欢
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-08-06
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
相关资源
最近更新 更多