1

The idea of this app is that the user types in something then clicks submit and the text field empties so they can type in something else then when they click a different button called randomised a label's text turns into a random item from the items the user entered. I thought I should append new inputs to an array and then create a randomIndex and then make the label's text a random item from the array using the randomIndex. Here's my code:

var choices = [""]

@IBOutlet weak var chosenLabel: UILabel!

@IBAction func randomiseButton(_ sender: Any) {
    let randomIndex = Int(arc4random_uniform(UInt32(self.choices.count)))
    let randomItem = self.choices[randomIndex]
    self.chosenLabel.text = "\(randomItem)"
}

@IBOutlet weak var enterLabel: UITextField!


@IBAction func submitButton(_ sender: Any) {
    let newItem = self.enterLabel.text
    self.choices.append(newItem!)
    self.enterLabel.text = ""
}

Thanks for all help in advance. Btw this doesn't bring up an error or anything but when I run this app on my iPad and enter things it works fine until I click randomise and then nothing happens. :(

5
  • You might want to set the max random number to .count-1 or else it'll be out of bounds when it randomly picks the highest number… Commented Apr 19, 2017 at 20:23
  • change you choices to var choices = [String]() because you want an empty array for storing your strings not an array with an empty string as its first item. Commented Apr 19, 2017 at 20:25
  • @LinusGeffarth the function return a value less than upper bound . check this link link Commented Apr 19, 2017 at 20:31
  • Oh I see, my bad. Thanks for the info @Alan Commented Apr 19, 2017 at 20:33
  • Is the randomise button properly connected to the IBAction? Does anything happen when you press the button if you add a breakpoint or a print(...) statement inside randomiseButton(_:)? Commented Apr 19, 2017 at 20:34

1 Answer 1

1

Ok, so this is quite embarrassing. I tried out other people's comments and none were working. @pdil said, "Is the randomise button properly connected to the IBAction?". I added a print function into that button and nothing was working so I knew the connection was wrong. It turned out I had my button hooked up to a label. I will put here the code I used in the end:

var choices = [String]()

@IBOutlet weak var chosenLabel: UILabel!

@IBAction func randomiseButton(_ sender: Any) {
    let randomIndex = Int(arc4random_uniform(UInt32(self.choices.count)))
    let randomItem = self.choices[randomIndex]
    self.chosenLabel.text = "\(randomItem)"
    print("testing")
}

@IBOutlet weak var enterLabel: UITextField!


@IBAction func submitButton(_ sender: Any) {
    let newItem = self.enterLabel.text
    self.choices.append(newItem!)
    self.enterLabel.text = ""
}

Sorry about this and thanks for all your help :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.