2

I'm attempting to build a class's blueprint in order to display the current date as i.e. "Saturday January 14, 2017" but I'm unable to create an array of calendar components in Swift 3. Will I need to break them up into separate variables like I've done for the year below and then combine each variable into a string?

Here is what I have so far that won't compile but from reading other's code it has worked in the past:

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

        }
    }
}
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61

1 Answers1

4

Maybe you may need to use dateComponents(_:from:) rather than component(_:from:):

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

But using DateFormatter seems to be a better choice.

OOPer
  • 47,149
  • 6
  • 107
  • 142