Help is needed. I need to make it so that 2 times a day (every 12 hours) the String in the UITextView, which I take from the finished array, changes to the next String in the array. Using Date or the Calendar function, I'm stuck. Offer your options, thanks in advance!

@IBOutlet var ThisTextView: UITextView! var array = ["a","b","c","d"] var lastDate: Date? var currentDate = Date() override func viewDidLoad() { super.viewDidLoad() if currentDate == lastDate { // Мы ничего не делаем } else { // Мы меняем string на следующий в массиве используя какую-то функцию/метод здесь или не здесь, как удобно. } 
  • Where do you get the data for lastDate? It is necessary to compare not dates, since it will be a very short point in time. - VAndrJ
  • While not from where, I can not understand how to do it all correctly, will you not help? - Gleb Valev
  • 2 times a day - at midnight and lunch? - VAndrJ
  • 9:00 and at 17:00 such time is necessary - Gleb Valev
  • If a user misses a couple of days, should he skip a few items or the next one? - VAndrJ

1 answer 1

This option will not be able to separate yesterday from today (if you need it), but thanks to it you can determine morning, afternoon, evening and night.

 //Создаем дефолтный грегорианский календарь let calendar = Calendar.init(identifier: .gregorian) //Теперь достаем массив компонентов даты, которые нам нужны и извлекаем из массива ЧАС let currentHour: Int = calendar.dateComponents([.hour], from: Date()).hour! /*Теперь, благодаря тому, что мы знаем текущий час, мы можем определить промежутки суток (хоть на каждый час свою функцию пили =) )*/ //со строгостью или нестрогостью неравенств можешь поиграть в зависимости от того, что ты хочешь if currentHour >= 9 && currentHour < 17 { // Этот блок кода исполняется, если сейчас от 9 до 17 часов //do something here } else { //Этот блок кода исполняется вечером, ночью и ранним утром //do something here } 
  • Wat, that's cool, thanks, I'm just trying to think of a function that will enumerate a String (from an array) in a UITextView, this is one of the big problems) - Gleb Valev
  • Could you clarify your task more? Maybe about the algorithm, I can help you. You have two time spans and 4 String selections in the array, right? What's next? - Gleb Kalachev
  • There is a UITextView in which something is written. The content of this textview should change at 9:00 and at 17:00 every day. Like all. The elements in the array will be greater than 4. I want to understand how to display this all on a UITextView using the code you wrote. All the more so that the Strings to be used do not start changing again (from the first element) when the application is minimized. - Gleb Valev