Objective-C中的.hash相当于Swift中的.hashValue吗?

时间:2016-04-16 16:50:31

标签: ios objective-c hash

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
        }
    }

1 个答案:

答案 0 :(得分:1)

这里有几个问题。

  1. NSDateFormatterStyle是一个枚举。您不应该使用指针来表示这些参数。
  2. hash仅可通过NSObject获取。因此,您无法在Objective-C中对原始类型调用hash
  3. hash会返回NSUInteger,但您的stringWithFormat正在使用%@说明符。那不行。 %@仅适用于对象指针。
  4. 您在方法名称中使用匿名参数是非标准的。
  5. 您的代码必须是这样的:

    + (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的内置类型(例如IntString等)提供支持。

    hash是来自NSObject的方法。它可以在Objective-C或Swift中使用,但仅限于扩展NSObject的类。它不能与原始类型一起使用。