我正在将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
}
}
答案 0 :(得分:2)
您的语法转换在语法上是正确的。
但是,NSDate
不提供名为formatterWithFormat:
或formatter
的静态方法;您正在翻译的代码似乎有一个扩展名方法。您需要找到该方法,然后翻译它。
注意:我强烈建议反对使其成为NSDate
的扩展名,因为它属于类层次结构的NSDateFormatter
方。
答案 1 :(得分:0)
根据您的所有更新,您的Objective-C代码和Swift代码之间的主要区别在于Swift函数为参数提供默认值。这允许您使用零个或多个参数调用函数(在本例中)。
由于Objective-C不支持默认参数值,因此您需要为每个参数传递一个值。
方法:
+ (NSDateFormatter *) formatter : (NSString *) format : (NSTimeZone*) timeZone : (NSLocale *) locale {
}
需要被称为:
[WhateverClassThisIs formatter:someFormat :someTimeZone :someLocale];
请注意由于您没有命名所有参数而导致的丑陋语法(就像我在上一个问题中所显示的那样。
您还需要删除添加的行,以便在方法开头为这些参数赋值。