如何在var声明中验证警卫
var completionTime: DateComponents{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm"
let selectedDateTime = mergeDateTime()
guard selectedDateTime != "" else {
return // return completionTime.self
}
// Do your Stuff
return differencesInDays
}
上面的变量将输入的完成时间差返回到当前日期时间
如果用户未能输入日期输入日期,我正在验证带护卫人员应在此处返回什么?
答案 0 :(得分:1)
通常的做法是使用Optional,以便在失败时返回nil。
var completionTime: DateComponents? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm"
let selectedDateTime = mergeDateTime()
guard selectedDateTime != "" else {
return nil
}
// Do your Stuff
return differencesInDays
}