作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
我是一名优秀的程序员,十分优秀!