简体   繁体   English

Objective-C 中的 .hash 等同于 Swift 中的 .hashValue 吗?

[英]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.这里有几个问题。

  1. NSDateFormatterStyle is an enum. NSDateFormatterStyle是一个枚举。 You should not use pointers for these parameters.您不应该对这些参数使用指针。
  2. hash is only available through NSObject . hash只能通过NSObject Therefore you can't call hash on primitive types in Objective-C.因此,您不能在 Objective-C 中对原始类型调用hash
  3. hash returns NSUInteger but your stringWithFormat is using the %@ specifier. hash返回NSUInteger但您的stringWithFormat正在使用%@说明符。 That won't work.那行不通。 %@ is only for object pointers. %@仅用于对象指针。
  4. Your use of anonymous parameters in the method name is non-standard.您在方法名称中使用匿名参数是非标准的。

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 的所有内置类型(如IntString等)支持。

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.

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