如何将此Swift语法转换为Objective-C?

用户名

我将练习将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
        }
    }
rmaddy

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

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

方法:

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

需要称为:

[WhateverClassThisIs formatter:someFormat :someTimeZone :someLocale];

请注意丑陋的语法,因为您没有命名所有参数(就像我在上一个问题中显示的那样)。

您还需要删除在方法开始时添加的为这些参数分配值的行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章