我正在练习快速,目前正在编写一个简单的函数来显示一天的时间表。如果return
语句包含一个值,则应仅包括busyDay
。我希望这可以自动完成,但不是。
func scheduleToday (date: String, day: String, numberOfMeetings: Int?) -> String {
var meetingMessage = ""
var busyDay: String!
if var meetings = numberOfMeetings {
if meetings > 4 {
busyDay = "Today is a busy day!"
}
meetingMessage = "\(meetings) meetings"
} else {
meetingMessage = "no meetings today."
}
return "| \(date) - \(day) | Good morning. \(busyDay) You have \(meetingMessage)."
}
如果
numberOfMeetings
为4或更小,则返回的字符串为:// | 17th of June - Wedensday | Good morning. nil You have 3 meetings.
最佳答案
您可以在if
中添加else语句,将budyDay设置为空字符串
if meetings > 4 {
busyDay = "Today is a busy day!"
} else {
busyDay = ""
}