Swift Error: Cannot use optional chaining on non-optional value of type 'Any'

Im trying to use this line of code:


func getFirstWeekDay() -> Int {

let day = ("\(currentYear)-\(currentMonthIndex)-01".date?.firstDayOfTheMonth.weekday)!

return day

}



but Im getting this error.. I dont know how else to write it.


Error: Cannot use optional chaining on non-optional value of type 'Any'

Accepted Reply

You have several errors. Where did you get the code from ?


First, you look for dtae property of a string :

"a string".date


There is no such property


Where did you define firstDayOfTheMonth ?


To get the date from a string, should use dateFormatter as here:

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let currentYear = 2020
let currentMonthIndex = "01"
let day = dateFormatter.date(from: "\(currentYear)-\(currentMonthIndex)-01")!


You set the day as 01. What the use to search for firstDaysOfTheMonth ?


You could also build the date through its components

let calendar = Calendar.current
var components = DateComponents()
components.day = 1
components.month = 1
components.year = 2020

let newDate = calendar.date(from: components)!


Then you can get weekDay

components = calendar.dateComponents([.day, .month, .year, .weekday], from: newDate)
print("weekday:\(components.weekday!)")


So, your func should become:


func getFirstWeekDay() -> Int {

    var components = DateComponents()
    components.day = 01
    components.month = currentMonthIndex
    components.year = currentYear
    let theDate = calendar.date(from: components)!
    components = calendar.dateComponents([.weekday], from: theDate)

    return components.weekday!
}


I advise you study Date class. This tutorial may help.

h ttp://ios-tutorial.com/working-dates-swift/

Replies

You have several errors. Where did you get the code from ?


First, you look for dtae property of a string :

"a string".date


There is no such property


Where did you define firstDayOfTheMonth ?


To get the date from a string, should use dateFormatter as here:

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let currentYear = 2020
let currentMonthIndex = "01"
let day = dateFormatter.date(from: "\(currentYear)-\(currentMonthIndex)-01")!


You set the day as 01. What the use to search for firstDaysOfTheMonth ?


You could also build the date through its components

let calendar = Calendar.current
var components = DateComponents()
components.day = 1
components.month = 1
components.year = 2020

let newDate = calendar.date(from: components)!


Then you can get weekDay

components = calendar.dateComponents([.day, .month, .year, .weekday], from: newDate)
print("weekday:\(components.weekday!)")


So, your func should become:


func getFirstWeekDay() -> Int {

    var components = DateComponents()
    components.day = 01
    components.month = currentMonthIndex
    components.year = currentYear
    let theDate = calendar.date(from: components)!
    components = calendar.dateComponents([.weekday], from: theDate)

    return components.weekday!
}


I advise you study Date class. This tutorial may help.

h ttp://ios-tutorial.com/working-dates-swift/

To get the date from a string, should use

dateFormatter
as here:

Please don’t do this. Setting a

dateFormat
string without first pinning the locale to
en_US_POSIX
will not end well. See QA1480 NSDateFormatter and Internet Dates for an explanation as to why.

You could also build the date through its components

This is a much better approach.

This tutorial may help.

And, of course, that tutorial has the same problem *sigh*

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I will definately look out for this. Thank you and thanks for the reference!!

This is from a old Swift 3 file that worked flawlessly but since updating it to Swift 4, 5 and doing the recommended fixes. It went haywire.


This is what I have defining the firstDayOfTheMonth:

Its also giving me these two errors:

'Declaration is only valid at file scope'

'Type 'String' has no member 'dateFormatter'




//get first day of the month

extension Date {

var weekday: Int {

return Calendar.current.component(.weekday, from: self)

}

var firstDayOfTheMonth: Date {

return Calendar.current.date(from: Calendar.current.dateComponents([.year,.month], from: self))!

}

}


//get date from string

extension String {

static var dateFormatter: DateFormatter = {

let formatter = DateFormatter()

formatter.dateFormat = "yyyy-MM-dd"

return formatter

}()


var date: Date? {

return String.dateFormatter.date(from: self)

}

}