2016-04-16 8 views
0

私は練習としてSwift LibraryをObjective-Cに変換しています。このSwiftの構文をObjective-Cに変換するには?

これをObjective-Cに変換するにはどうすればよいですか?

let formatter = NSDate.formatter(format: dateFormat) 

は、私が試した:

NSDateFormatter *formatter = [NSDate formatterWithFormat : dateFormat]; 

。また、これを試してみました:

NSDateFormatter *formatter = [NSDate formatter : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]]; 

エラー:なし既知のクラスメソッドセレクタの「formatterWithFormatもこれを試してみました

NSDateFormatter *formatter = [NSDate formatterWithFormat : dateFormat : [NSTimeZone localTimeZone] : [NSLocale currentLocale]]; 

: "または"フォーマッタ::: "

もっとコンテキスト:

+ (NSDateFormatter *) formatter : (NSString *) format : (NSTimeZone*) timeZone : (NSLocale *) locale { 
    format = DefaultFormat; 
    timeZone = [NSTimeZone localTimeZone]; 
    locale = [NSLocale currentLocale]; 
    NSString *hashKey = [NSString stringWithFormat:@"%lu%lu%lu", (unsigned long)format.hash, (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.dateFormat = format; 
     formatter.timeZone = timeZone; 
     formatter.locale = locale; 
     formatters[hashKey] = formatter; 
     return formatter; 
    } 
} 

ので、SO、私はより多くのコードを追加できるようになるには、間にいくつかのランダムなテキスト...

+ (NSDateFormatter *)formatter:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle relativeDateFormatting:(BOOL)doesRelativeDateFormatting timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale { 
    timeZone = [NSTimeZone localTimeZone]; 
    locale = [NSLocale currentLocale]; 
    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; 
    } 
} 

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

private class func formatter(format format:String = DefaultFormat, timeZone: NSTimeZone = NSTimeZone.localTimeZone(), locale: NSLocale = NSLocale.currentLocale()) -> NSDateFormatter { 
      let hashKey = "\(format.hashValue)\(timeZone.hashValue)\(locale.hashValue)" 
      var formatters = NSDate.sharedDateFormatters() 
      if let cachedDateFormatter = formatters[hashKey] { 
       return cachedDateFormatter 
      } else { 
       let formatter = NSDateFormatter() 
       formatter.dateFormat = format 
       formatter.timeZone = timeZone 
       formatter.locale = locale 
       formatters[hashKey] = formatter 
       return formatter 
      } 
     } 

その間にいくつかのランダムなテキストがありますので、より多くのコードを追加できます...

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

これが呼び出すメソッドであれば、3つのパラメータすべてを渡す必要があります。 – rmaddy

+0

そして、それらの値を単純に上書きした場合、これらの3つのパラメータが渡される点は何ですか?前回の質問で私があなたに与えたコードは使用していませんでした。 – rmaddy

+0

実際、私はしました。これを質問に追加してみましょう。私はこれを既存のSwiftコードから変換し、できるだけ似たように保つことを心がけてください。 –

答えて

0

すべての更新情報に基づいて、あなたのObjective-Cのコードとスウィフトコードの主な違いは、スウィフト関数はパラメータのデフォルト値を提供することです。これにより、関数(この場合)を0以上の引数で呼び出すことができます。

のObjective-Cは、デフォルトのパラメータ値をサポートしていないので、あなたはすべてのパラメータの値を渡す必要があります。

方法:

+ (NSDateFormatter *) formatter : (NSString *) format : (NSTimeZone*) timeZone : (NSLocale *) locale { 
} 

ニーズと呼ばれる:

[WhateverClassThisIs formatter:someFormat :someTimeZone :someLocale]; 

注私はあなたの前の質問で示したようにあなたのために醜い構文は(すべてのパラメータの名前を付けないよう

また、メソッドの最初にこれらのパラメータに値を割り当てるために追加した行を削除する必要があります。

+0

私はこれを試しました:NSDateFormatter * formatter = [NSDate formatterWithFormat:dateFormat:[NSTimeZone localTimeZone]:[NSLocale currentLocale]];私はまだ同じエラーが発生します。 –

+0

私の更新された答えを見てください。 – rmaddy

+0

BTW(これは役に立つと思われる建設的なコメントです) - ここ数日間質問したすべての質問を考慮して、Objective-Cプログラミング言語に関する良いチュートリアルを見つけることを強くお勧めします。構文の学習に費やされた時間は、あなたが進むにつれて時間の節約になります。 – rmaddy

2

あなたの構文の変換は構文的に正しいです。

ただし、NSDateには、formatterWithFormat:またはformatterという静的メソッドはありません。翻訳元のコードにという拡張子があるようです。その方法を見つけて、代わりにそれを翻訳する必要があります。

注:それはクラス階層のNSDateFormatter側に属しているので、私は強く、それをNSDateの拡張を行うことに対するをお勧めします。

+0

私はそのメモに同意します。このようなメソッドをNSDateクラスに持たせることは意味がありません。 – rmaddy

+0

私はもっと文脈を提供する必要があると思います。私は私の質問を編集します。 –

関連する問題