0

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.

James C
  • 7
  • 2

2 Answers2

2

If you want to add a new line of text for each time an if condition is true you need to append the text and use a new line character, \n

while (playerDead != true) {
//...
    if playerOnePosition != playerTwoPosition {
        labelText.text = 
             (labelText.text ?? "" ) + 
             "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)\n"
    }

    if playerOnePosition == playerTwoPosition {
        labelText.text = (labelText.text ?? "" ) + "\(timeStamp) They are in the same place."
        playerDead = true  
    } 
}

Also to get your label to display any number of lines set the limit to 0

labelText.numberOfLines = 0
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Yes! That worked! I don't understand the (labelText.text ?? " ") part though. What does that do exactly? +1 Thanks! – James C Jul 21 '19 at 15:30
  • `text` is an optional property, that is it might return nil. Using ?? after an optional property means it will return the value to the right if the property is nil so in this case an empty string (so we can append). Please mark the answer as accepted if it helped you – Joakim Danielson Jul 21 '19 at 15:34
0

You are setting the text of the label each time, instead of appending to it. Replace this line:

labelText.text = "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"

with this:

labelText.text += "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"

and replace this line:

labelText.text = "\(timeStamp) They are in the same place."

with this:

labelText.text += "\(timeStamp) They are in the same place."

Note that the only difference is that I changed your + operators to += operators.

Hope this helps!

Sam
  • 2,350
  • 1
  • 11
  • 22
  • Adding "+=" to labelText.text gives me an error of "Expression type '@lvalue String?' is ambiguous without more context". – James C Jul 21 '19 at 13:45