2017-01-15 11 views
1

"2017年1月14日土曜日"のように現在の日付を表示するためにクラスの青写真を作成しようとしていますが、配列を作成できませんSwift 3のカレンダーコンポーネントを使用しています。下の1年間で行ったように別々の変数に分割し、各変数を文字列に結合する必要がありますか?ここでSwift 3の配列リテラルではCalendar.componentsを使用できません

コンパイルされませんよう、私がこれまで持っているものですが、相手のコードを読んでから、それは、過去に働いています

import Foundation 
class CurrentCalendarDate { 
    var currentDateString: String { 
     get{ 
      let date = Date() 

      let calendar = Calendar.current 

      //let components = calendar.component(.year, from: date) 

      let componetsThatWeWant = calendar.component([.year, .day, .month], from: date) 

      let readableDate: String = "Today's date is: \(componetsThatWeWant.day) \(componetsThatWeWant.month), \(componetsThatWeWant.year)" 

      return readableDate 

     } 
    } 
} 
+1

'var currentDateString:Int {'はIntを返します???意味がない –

+2

あなたはDateFormatterを使用することになっています。 'formatter.dateStyle = .full' –

+1

これを参考にして、shortの代わりにfullにすることができます。 http://stackoverflow.com/a/28347285/2303865 –

答えて

3

たぶん、あなたはむしろcomponent(_:from:)よりdateComponents(_:from:)を使用する必要があります。

let componetsThatWeWant = calendar.dateComponents([.year, .day, .month], from: date) 

しかし、DateFormatterを使用すると、より良い選択と思われます。

関連する問題