My program has a while-loop that runs some code, generating various text statements as it goes along. The problem is that the UILabel only prints the last line of text in the series (my understanding is because it iterates too quickly). How do I get the label to print all of the text encountered, like one would see in console output?
I looked at this link but the example doesn't seem to match my situation and not sure how to implement the fix (if that's even the right one): Update Label In While Loop Swift
class ViewController: UIViewController {
var locationArray = ["Place A", "Place B", "Place C", "Place D"]
var timeStamp = 0
var playerDead = false
@IBOutlet var labelText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
} //end viewDidLoad
@IBAction func startGame(_ sender: Any) {
var playerOnePosition = locationArray.randomElement()!
var playerTwoPosition = locationArray.randomElement()!
while (playerDead != true) {
playerOnePosition = locationArray.randomElement()!
playerTwoPosition = locationArray.randomElement()!
timeStamp += 1
if playerOnePosition != playerTwoPosition {
labelText.text = "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
} //End first if statement
if playerOnePosition == playerTwoPosition {
labelText.text = "\(timeStamp) They are in the same place."
playerDead = true //Game ends here
} //End second if statement
} //End while loop
} //End function
} //End class
An example of usual output would be "13 They are in the same place", but I want the UIlabel to print all of the other "timestamp" events leading up to 13.