gpt4 book ai didi

swift - 如何使用日期或日历更改UITextView中的值?

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

我有一个 UITextView,我需要每天更改数组中的下一个“字符串”2 次(每 12 小时)。我这里有一些数组需要与 Date 和 UITextView 一起使用。我需要如何使用日历?我的简单问题是“我需要做什么?”

@IBOutlet var ThisTextView: UITextView!
var array = ["a","b","c","d"]
var lastDate: Date?
var currentDate = Date()

override func viewDidLoad() {
super.viewDidLoad()

if currentDate == lastDate {
// we do nothing
} else {
// we change string to the next in array using some function
// or we write this function here
}

最佳答案

这是我想出的代码:

// This is YOUR "lastDate", please supply your own value
let mySuppliedDate = Date()

// You should have your own date to compare to and convert it to seconds
let compareDate:Int = Int(mySuppliedDate.timeIntervalSince1970)

// Current date (today's date)
let currentDate: Date = Date()


let dateFormatter: DateFormatter = DateFormatter()
// Customize the dateStyle property to suit your needs (date format)
dateFormatter.dateStyle = .medium

// This will give you the current time in seconds, or what is known as the (UNIX) Epoch time
// We cast it to an Int because it returns a Double
let currentDateInSecs: Int = Int(currentDate.timeIntervalSince1970)

// Formula: seconds * minutes per hour * no. of hrs
let triggerTime = 60 * 60 * 12

if (currentDateInSecs - compareDate) >= triggerTime {
// 12 hrs or more have passed

// Update the Label
ThisTextView.text = dateFormatter.string(from: currentDate)

}

// else less than 12 hrs have passed

// We do nothing

请注意,您必须向 mySuppliedDate 提供您自己的值。

希望这会有所帮助!

关于swift - 如何使用日期或日历更改UITextView中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46162659/

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