2016-04-16 8 views
1

Objective-Cの.hashはSwiftの.hashValueに相当しますか?Objective-Cの.hashはSwiftの.hashValueと同じですか?

もしそうでない場合、Objective-Cには.hashValueと同等のものはありますか?これは私がここに持っている問題に関し、

は(私は運動としてのObjective-Cにスウィフトライブラリを変換しています):NSString *hashKey = [NSString stringWithFormat:@"%@%@%@%@%@", dateStyle.hash, timeStyle.hash, doesRelativeDateFormatting.hash, timeZone.hash, locale.hash];

加盟基準基地タイプ:

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

は、この行に私を指示します"NSDateFormatterStyle "(別名 "列挙型NSDateFormatterStyle")構造体または共用体ではありません

オリジナルスウィフトコード:

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

'hash'は' NSObject'に由来します。プリミティブ型に対して 'hash'を呼び出すことはできません。 – rmaddy

+1

そして 'NSDateFormatterStyle'は列挙型です。ポインタとして渡すべきではありません。 – rmaddy

+1

ハッシュは一意ではありません。ハッシュ値に基づいて一意のキーを作成する必要があるようです。それは適切ではありません。 – rmaddy

答えて

1

ここにいくつかの問題があります。

  1. NSDateFormatterStyleはenumです。これらのパラメータにはポインタを使用しないでください。
  2. hashは、NSObjectからのみ入手できます。したがって、Objective-Cのプリミティブ型でhashを呼び出すことはできません。
  3. hashstringWithFormat%@指定子を使用していますが、NSUIntegerが返されます。それは動作しません。 %@は、オブジェクトポインタ専用です。
  4. メソッド名に匿名パラメータを使用すると、非標準です。 hashhashValue対質問に関しては

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

あなたのコードは次のようなものである必要があります。彼らは同じことです。スウィフトはHashableプロトコル経由hashValueを提供し、など

hashNSObjectからの方法で、IntStringなどのスウィフトの組み込み型のすべてをサポートしているが。 Objective-CまたはSwiftでは使用できますが、拡張するクラスはNSObjectです。プリミティブ型では使用できません。

関連する問題