我有这样的方法。
func getFirstDayX(date:Date)->CGFloat
{
// let date = NSDate()
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEEE"//"EE" to get short style
guard let dayInWeek = dateFormatter.string(from: (date as? Date)!) else
{
return 0.0
}//"Sunday"
let dayX:CGFloat!
print("day in week-------\(dayInWeek)")
switch dayInWeek {
case "Monday":
dayX=0.0
case "Tuesday":
dayX=lblDayName.frame.size.width
case "Wednesday":
dayX=lblDayName.frame.size.width*2
case "Thursday":
dayX=lblDayName.frame.size.width*3
case "Friday":
dayX=lblDayName.frame.size.width*4
case "Saturday":
dayX=lblDayName.frame.size.width*5
case "Sunday":
dayX=lblDayName.frame.size.width*6
default:
dayX=0.0
}
return dayX
}
但我收到一个错误
条件绑定的初始化程序必须具有可选类型,而不是“字符串”
这会是什么原因?请帮我。
更新
这是我添加语言环境和保护条件之前的方式。请检查这个。
func getFirstDayX(date:Date)->CGFloat
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"//"EE" to get short style
let dayInWeek = dateFormatter.string(from: date as Date) else
let dayX:CGFloat!
print("day in week-------\(dayInWeek)")
switch dayInWeek {
case "Monday":
dayX=0.0
case "Tuesday":
dayX=lblDayName.frame.size.width
case "Wednesday":
dayX=lblDayName.frame.size.width*2
case "Thursday":
dayX=lblDayName.frame.size.width*3
case "Friday":
dayX=lblDayName.frame.size.width*4
case "Saturday":
dayX=lblDayName.frame.size.width*5
case "Sunday":
dayX=lblDayName.frame.size.width*6
default:
dayX=0.0
}
return dayX
}