Objective-C中的.hash
相当于Swift中的.hashValue
吗?
如果没有,Objective-C中的.hashValue
相当于什么?
这与我在这里遇到的问题有关(我正在将Swift库转换为Objective-C作为练习):
+ (NSDateFormatter *) formatter : (NSDateFormatterStyle *) dateStyle : (NSDateFormatterStyle *) timeStyle : (BOOL) doesRelativeDateFormatting : (NSTimeZone *) timeZone : (NSLocale *) locale {
timeZone = [NSTimeZone localTimeZone];
locale = [NSLocale currentLocale];
NSString *hashKey = [NSString stringWithFormat:@"%@%@%@%@%@", dateStyle.hash, timeStyle.hash, doesRelativeDateFormatting.hash, timeZone.hash, 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;
}
}
告诉我这一行:NSString *hashKey = [NSString stringWithFormat:@"%@%@%@%@%@", dateStyle.hash, timeStyle.hash, doesRelativeDateFormatting.hash, timeZone.hash, locale.hash];
成员引用基类型“NSDateFormatterStyle ”(又名“enum NSDateFormatterStyle ”)不是结构或联合
原始Swift代码:
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 :(得分:1)
这里有几个问题。
NSDateFormatterStyle
是一个枚举。您不应该使用指针来表示这些参数。hash
仅可通过NSObject
获取。因此,您无法在Objective-C中对原始类型调用hash
。hash
会返回NSUInteger
,但您的stringWithFormat
正在使用%@
说明符。那不行。 %@
仅适用于对象指针。您的代码必须是这样的:
+ (NSDateFormatter *)formatter:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle relativeDateFormatting:(BOOL)doesRelativeDateFormatting timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
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;
}
}
关于hash
vs hashValue
问题。它们有点相同。虽然Swift通过hashValue
协议提供Hashable
,但它通过所有Swift的内置类型(例如Int
和String
等)提供支持。
hash
是来自NSObject
的方法。它可以在Objective-C或Swift中使用,但仅限于扩展NSObject
的类。它不能与原始类型一起使用。