[英]Is .hash in Objective-C equivalent to .hashValue in Swift?
Is .hash
in Objective-C equivalent to .hashValue
in Swift ?是.hash
在Objective-C相当于.hashValue
斯威夫特?
If not, what is the equivalent of .hashValue
in Objective-C ?如果不是,那么 Objective-C 中.hashValue
的等价物是什么?
This relates to an issue I have here ( I am converting a Swift Library into Objective-C as an exercise) :这与我在这里遇到的一个问题有关(我将 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;
}
}
Tells me on this line: NSString *hashKey = [NSString stringWithFormat:@"%@%@%@%@%@", dateStyle.hash, timeStyle.hash, doesRelativeDateFormatting.hash, timeZone.hash, locale.hash];
在这一行告诉我: NSString *hashKey = [NSString stringWithFormat:@"%@%@%@%@%@", dateStyle.hash, timeStyle.hash, doesRelativeDateFormatting.hash, timeZone.hash, locale.hash];
Member reference base type "NSDateFormatterStyle " (aka "enum NSDateFormatterStyle ") is not a structure or union成员引用基类型“NSDateFormatterStyle ”(又名“enum NSDateFormatterStyle ”)不是结构或联合
Original Swift code:原始 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
}
}
There are several issues here.这里有几个问题。
NSDateFormatterStyle
is an enum. NSDateFormatterStyle
是一个枚举。 You should not use pointers for these parameters.您不应该对这些参数使用指针。hash
is only available through NSObject
. hash
只能通过NSObject
。 Therefore you can't call hash
on primitive types in Objective-C.因此,您不能在 Objective-C 中对原始类型调用hash
。hash
returns NSUInteger
but your stringWithFormat
is using the %@
specifier. hash
返回NSUInteger
但您的stringWithFormat
正在使用%@
说明符。 That won't work.那行不通。 %@
is only for object pointers. %@
仅用于对象指针。Your code needs to be something like this:你的代码需要是这样的:
+ (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;
}
}
With regard to the hash
vs hashValue
question.关于hash
vs hashValue
问题。 They are sort of the same thing.他们是一样的东西。 Though Swift provides the hashValue
through the Hashable
protocol and it is supported through all of Swift's built-in types like Int
and String
, etc.尽管 Swift 通过Hashable
协议提供了hashValue
并且它被 Swift 的所有内置类型(如Int
和String
等)支持。
hash
is a method from NSObject
. hash
是来自NSObject
一个方法。 It can be used in Objective-C or Swift but only on classes that extend NSObject
.它可以在 Objective-C 或 Swift 中使用,但只能用于扩展NSObject
类。 It can't be used with primitive types.它不能与原始类型一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.