我正在快速练习,目前正在编写一个简单的功能来显示当天的日程安排。如果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
如果// | 17th of June - Wedensday | Good morning. nil You have 3 meetings.
为4或更小,则返回的字符串显示为:
Summary
-----------
1. In main() am going for pthread_cond_wait().
2. In signal handler() am waking main() using pthread_cond_signal().
3. But main() is not coming out from pthread_cond_wait().
答案 0 :(得分:0)
你可以在if
添加一个else语句,将budyDay设置为空字符串
if meetings > 4 {
busyDay = "Today is a busy day!"
} else {
busyDay = ""
}
答案 1 :(得分:0)
如果ternary operator
为4或更少且您的代码为:
numberOfMeetings
分配空字符串
func scheduleToday (date: String, day: String, numberOfMeetings: Int?) -> String {
var meetingMessage = ""
var busyDay: String!
if var meetings = numberOfMeetings {
busyDay = meetings > 4 ? "\(meetings) meetings" : ""
meetingMessage = "\(meetings) meetings"
} else {
meetingMessage = "no meetings today."
}
return "| \(date) - \(day) | Good morning. \(busyDay) You have \(meetingMessage)."
}
let schedule = scheduleToday("1234", "8678678", 3) // "| 1234 - 8678678 | Good morning. You have 3 meetings.""