Show array being traversed

Hello, I am trying to display all the content of my array to a label right before a random element is selected and displayed last. I tried the sleep function but it just slowed my whole app down and only displayed the last element. I'm probably doing something wrong with it:
while i<Places
{
label.text = Places[i]
sleep(1)
i +=1
}
Please try to explain your context precisely.
Are you writing an iOS app in Swift? Your code would not compile if it were a Swift code.

Please show enough code if you cannot explain your context completely, whole code of your view controller would be helpful
Yes, I am trying to write an IOS app in Swift. It compiles and runs without any warnings or errors. My main goal is for the contents of the array to briefly show onto the label right before the random element that was selected is displayed. Pretty much like a randomizer does right before displaying the random element. The whole functions is:
var prev = -1
   @IBAction func Random()
   {
     let Places = ["Burger King","Chick-Fil-A","McDonald's","Wendys","Checkers","TacoBell","PDQ","Pizza Hut","Jimmy John's","Subway","Dominos","Pollo Tropical","Chipotle","Olive Garden","Ale House","Five Guys","Fresh Kitchen","Blaze","Outback"]
     
     label.isHidden = false
     var i = 0
     var number = Int.random(in: 0..<19)
     
     if(prev == number)
     {
       number = Int.random(in: 0..<19)
     }
     while i<Places.count
     {
       label.text = Places[i]
       sleep(UInt32(0.99))
       i+=1
     }
     
     label.text = Places[number]
     prev = number
   }


The whole functions is:

Thanks for showing the code, things get clearer with it.

General advise for using the dev forums, you should better include tags Swift and UIKit, when you ask some programming issue about an iOS app using UIKit and Swift. (The tag Xcode is not needed unless you want to ask how to use Xcode itself.)

And better use the Code block feature of this site, shown as < > in the tool bar of the editing area.

And one note about coding convention of Swift, in Swift, only type names start with Capital letter, your method Random or your local variable Player looks very odd for Swift programmers and takes more time to read it.


And one of the most important things in iOS programming,

Never call sleep in an action method.

(Never use any other blocking functions in the main thread.)

Actual UI updates begin after the action method is finished. So your code just delays the UI updates and shows the result of the last thing you have done in the method.

You may need to work with Timer or the background threads.
An example:
Code Block
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
//...
var prev = -1
@IBAction func random(_ sender: Any) {
let places = ["Burger King","Chick-Fil-A","McDonald's","Wendys","Checkers","TacoBell","PDQ","Pizza Hut","Jimmy John's","Subway","Dominos","Pollo Tropical","Chipotle","Olive Garden","Ale House","Five Guys","Fresh Kitchen","Blaze","Outback"]
label.isHidden = false
var i = 0
var number = Int.random(in: 0..<19)
if prev == number {
number = Int.random(in: 0..<19)
}
let now = Date()
let timer = Timer(fire: now, interval: 0.2, repeats: true) {timer in
if i < places.count {
self.label.text = places[i]
i += 1
} else {
self.label.text = places[number]
self.prev = number
timer.invalidate()
}
}
RunLoop.main.add(timer, forMode: .default)
}
}


Thank you for the tips! I'm attempting to teach myself how to make an app and that will certainly help with feature projects.
I tried adding the code segment into my program, and when I tap my random button, it ends up crashing and throwing the error:
Thread 1: "-[Foodapp.LazyViewController Random]: unrecognized selector sent to instance 0x7ffaa3415670" terminating with uncaught exception of type NSException
Accepted Answer

Thread 1: "-[Foodapp.LazyViewController Random]: unrecognized selector sent to instance 0x7ffaa3415670" terminating with uncaught exception of type NSException 

Thanks for reporting.

As you see, I renamed Random() to random(_:).
When you renamed a once connected IBAction or IBOutlet, you need to disconnect and re-connect it to the updated method.

It may be easier that you remove the old button and add a new button and connect the action of the new button to random(_:).
Ahh, That worked! Thank you so much for your help!
Show array being traversed
 
 
Q