2021年1月19日星期二

How can I tie independent values to strings in an array (Swift)?

So I'm practicing coding and I've set up an array of strings. What I want to happen is to have a function randomly select a string and if a certain string is selected, it would print out one message, and if any of the other strings are selected, it would print out a different one. I think setting up the messages could be done w/an if-else function, but I'm stuck on the earlier step of figuring out how to identify which string is called.

I did some searching and found this thread Get Random String from an Array and I copied the randomization code from it. I'm a bit overwhelmed with where to go from there.

What type of function/identifier etc would I need to set up so that the strings are uniquely identified?

(Apologies if the code doesn't format right, still trying to figure it out).

class ViewController: UIViewController {    override func viewDidLoad() {      super.viewDidLoad()        }    // Make collection of games and if Monsters is called, print "Take a break." If a diff string is called, print "Do your homework."      @IBAction func random(_ sender: UIButton) {          let games = ["Cards Against Humanity", "Mario Kart", "Halo", "Pixeljunk Monsters"]          let randomNumber = Int(arc4random_uniform(UInt32(games.count)))          _ = games[randomNumber]      }  

} // end of class

https://stackoverflow.com/questions/65802950/how-can-i-tie-independent-values-to-strings-in-an-array-swift January 20, 2021 at 12:07PM

没有评论:

发表评论