简体   繁体   中英

How to convert this Swift syntax into Objective-C?

I am converting a Swift Library into Objective-C as an exercise.

How to convert this into Objective-C ?

let formatter = NSDate.formatter(format: dateFormat)

I tried:

NSDateFormatter *formatter = [NSDate formatterWithFormat : dateFormat];

Also tried this:

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

Also tried this:

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

Error: no know class method for selector "formatterWithFormat:" or "formatter :::"

More context:

+ (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;
    }
}

Some random text in-between so SO will let me add more code...

+ (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;
    }
}

Original Swift code:

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
            }
        }

Some random text in-between so SO will let me add more code...

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
        }
    }

Your conversion of the syntax is syntactically correct.

However, NSDate does not offer a static method called formatterWithFormat: or formatter ; it appears that the code from which you are translating has an extension method for that. You need to locate that method, and translate it instead.

Note: I would strongly recommend against making it an extension on NSDate , because it belongs to NSDateFormatter side of class hierarchy.

Based on all your updates, the key difference between your Objective-C code and Swift code is that the Swift function provides default values for the parameters. This allows you to call the function (in this case) with zero or more arguments.

Since Objective-C doesn't support default parameter values, you need to pass a value for every parameter.

The method:

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

Needs to be called as:

[WhateverClassThisIs formatter:someFormat :someTimeZone :someLocale];

Note the ugly syntax due to you not naming all of the parameters (like I showed in your previous question.

You also need to remove the lines you added to assign values to those parameters at the start of the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM