gpt4 book ai didi

swift - 如何将函数返回到标签 StringValue

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

func GetTheTime() {
let calendar = NSCalendar.current
let date = Date()
_ = Calendar.current

let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
print("\(hour):\(minutes):\(seconds)")
var theTime = "\(hour):\(minutes):\(seconds)"

如何调用它并让它返回并显示到 label.StringValue 中?

最佳答案

要返回某些内容,您可以添加一个箭头以及您想要返回的数据类型。就像这样:

func getTheTime() -> String {
let calendar = NSCalendar.current
let date = Date()
_ = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
return "\(hour):\(minutes):\(seconds)"
}

然后您可以像使用字符串一样使用 getTheTime() 函数,这样您就可以执行以下操作:

myLabel.text = getTheTime()

如果您希望它始终显示以秒为单位的更新时间,则必须调用一个定时函数,您可以在堆栈溢出上找到该函数的答案。

关于swift - 如何将函数返回到标签 StringValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144219/

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