如何将此Swift语法转换为Objective-C?
作为练习,我正在将Swift库转换为Objective-C 如何将其转换为Objective-C如何将此Swift语法转换为Objective-C?,objective-c,swift,Objective C,Swift,作为练习,我正在将Swift库转换为Objective-C 如何将其转换为Objective-C let formatter = NSDate.formatter(format: dateFormat) 我试过: NSDateFormatter *formatter = [NSDate formatterWithFormat : dateFormat]; 我也试过: NSDateFormatter *formatter = [NSDate formatterWithFormat : date
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]];
我也试过:
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;
}
}
一些随机文本之间,所以让我添加更多的代码
原始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
}
}
一些随机文本之间,所以让我添加更多的代码
语法的转换在语法上是正确的 但是,
NSDate
不提供名为formatterWithFormat:
或formatter
的静态方法;您正在翻译的代码似乎有一个扩展方法。您需要定位该方法,并转换它
注意:我强烈建议不要将其作为
NSDate
的扩展,因为它属于类层次结构的NSDateFormatter
端。语法转换是正确的
但是,NSDate
不提供名为formatterWithFormat:
或formatter
的静态方法;您正在翻译的代码似乎有一个扩展方法。您需要定位该方法,并转换它
注意:我强烈建议不要将其作为
NSDate
的扩展,因为它属于类层次结构的NSDateFormatter
端。根据您的所有更新,Objective-C代码和Swift代码之间的关键区别在于Swift函数为参数提供了默认值。这允许您使用零个或多个参数调用函数(在本例中)
由于Objective-C不支持默认参数值,因此需要为每个参数传递一个值
方法:
+ (NSDateFormatter *) formatter : (NSString *) format : (NSTimeZone*) timeZone : (NSLocale *) locale {
}
需要称为:
[WhateverClassThisIs formatter:someFormat :someTimeZone :someLocale];
请注意,由于没有命名所有参数,语法很难看(如我在前面的问题中所示)
您还需要删除在方法开始时为这些参数赋值而添加的行。根据您的所有更新,Objective-C代码和Swift代码之间的关键区别在于Swift函数为参数提供默认值。这允许您调用函数(在本例中)具有零个或多个参数 由于Objective-C不支持默认参数值,因此需要为每个参数传递一个值 方法:
+ (NSDateFormatter *) formatter : (NSString *) format : (NSTimeZone*) timeZone : (NSLocale *) locale {
}
需要称为:
[WhateverClassThisIs formatter:someFormat :someTimeZone :someLocale];
请注意,由于没有命名所有参数,语法很难看(如我在前面的问题中所示)
您还需要删除在方法开始时为这些参数赋值而添加的行。我同意注释。在
NSDate
类上使用这样的方法是没有意义的。我想我需要提供更多的上下文。我将编辑我的问题。我同意注释。在时区和区域设置的前两行。这些行是没有意义的,因为您提供了所需的值作为参数。不过,您还是得到了答案g错误,因为您只传递一个参数,而不是所有必需的参数。如果这是您尝试调用的方法,那么您需要传递所有3个参数,而不仅仅是一个。如果您只是覆盖这三个参数的值,那么传递这三个参数有什么意义?您没有使用我在以前的qu中给您的代码估计。事实上,我做了。让我将此添加到问题中。请记住,我正在从现有的Swift代码转换此代码,并尝试使其尽可能相似。我删除了设置时区和区域设置的前两行。这些行是没有意义的,因为您提供了所需的值作为参数。但是,您仍然是g正在设置错误,因为您只传递了一个参数,而不是所有必需的参数。好的,我尝试了以下操作:NSDateFormatter*formatter=[NSDate formatterWithFormat:dateFormat:[NSTimeZone localTimeZone]:[NSLocale currentLocale]];但仍然收到相同的错误。顺便说一句(请将此视为有益的建设性意见)-考虑到你在过去几天里提出的所有问题,我强烈建议你找到一本关于Objective-C编程语言的好教程。现在花在学习语法上的时间将为你节省大量的时间。哈哈哈!事实上,我专门将Swift库转换为Objective-C来学习语法(在一些教程和整个课程之后)。但是谢谢你的评论!顺便说一句:仍然不起作用。NSDateFormatter*formatter=[NSDate formatter:dateFormat:[NSTimeZone localTimeZone]:[NSLocale currentLocale]];你把这个+formatter::
方法添加到哪个类?好的,我尝试了这个:NSDateFormatter*formatter=[NSDate formatterWithFormat:dateFormat:[NSTimeZone localTimeZone]:[NSLocale currentLocale]];但我仍然收到相同的错误。顺便说一句(请将此作为有用的、建设性的注释)-考虑到你在过去几天里提出的所有问题,我强烈建议你找到一本关于Objective-C编程语言的好教程。现在花在学习语法上的时间将为你节省大量的时间。哈哈哈!事实上,我专门将Swift库转换为Objective-C来学习语法