gpt4 book ai didi

swift - 如何使标签对指定日期使用不同的字符串?

转载 作者:行者123 更新时间:2023-11-30 12:45:55 25 4
gpt4 key购买 nike

我的想法是制作一个 ios 应用程序来显示我工作中今天的午餐。我不知道如何解决这个问题。我自己的想法是拥有一个 UIDatePicker (默认设置为当前日期)并具有响应不同日期的函数。这是一些代码,只是为了说明我脑海中的想法。

var dateFromPicker = UIDatePicker.date
@IBOutlet weak var lunchLabel: UILabel!

func februaryFirst {
let dateFebruaryFirst = ...
if dateFromPicker = dateFebruaryFirst {
lunchLabel.text = ("Fish'n chips")
}
}

func februarySecond {
let dateFebruarySecond = ...
if dateFromPicker = dateFebruarySecond {
lunchLabel.text = ("Noodlesoup")
}
}

最佳答案

您可能想使用 DateComponents 来检查某个日期是哪一天/哪一个月。例如:

func februarySecond {
// Get the day and month of the given date
let dateFromPickerComponents = Calendar.current.dateComponents([.day, .month], from: dateFromPicker)
// Check whether the day and month match Feb 2
if dateFromPickerComponents.day == 2 && dateFromPickerComponents.month == 2 {
lunchLabel.text = ("Noodlesoup")
}
}

关于swift - 如何使标签对指定日期使用不同的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41650068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com