简体   繁体   中英

Is .hash in Objective-C equivalent to .hashValue in Swift?

Is .hash in Objective-C equivalent to .hashValue in Swift ?

If not, what is the equivalent of .hashValue in Objective-C ?

This relates to an issue I have here ( I am converting a Swift Library into Objective-C as an exercise) :

+ (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];

Member reference base type "NSDateFormatterStyle " (aka "enum NSDateFormatterStyle ") is not a structure or union

Original Swift code:

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. You should not use pointers for these parameters.
  2. hash is only available through NSObject . Therefore you can't call hash on primitive types in Objective-C.
  3. hash returns NSUInteger but your stringWithFormat is using the %@ specifier. 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. 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.

hash is a method from NSObject . It can be used in Objective-C or Swift but only on classes that extend NSObject . It can't be used with primitive types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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