Tom487
Tom487

Reputation: 31

How do I get a label to display random text when a button is pressed? [Swift]

I'm currently developing my first app, and i'm completely stuck with generating random text in a label when a button is pressed. Here's a bit more information:

the app is a very simply studying tool which will help me remember quotes from famous historians (i know i could use a pen and paper but this way is more fun and my friends can use it too).

Here's what i have:

@IBAction func quoteButtonTapped(sender: AnyObject) {
    var randomNumber = arc4random_uniform(5) + 1
    var QuoteString:String = String(format: "quote%i", randomNumber)

    self.quoteLabel.text = QuoteString

    var quote0 = "Quote 000"
    var quote1 = "Quote 001"
    var quote2 = "Quote 002"
    var quote3 = "Quote 003"
    var quote4 = "Quote 004"
    var quote5 = "Quote 005"
    var quote6 = "Quote 006"
}

(Code image found here)

So as you can see, the random generator adds a number to the prefix 'quote' and then the label (quoteLabel) displays the outcome 'Quotestring' (The prefix and the random number put together)

However, what happens is the label just displays the text 'quote1' or 'quote3' and not the variable that i have attached to those "Quote 000"

How do i get the Label to display the variables ("Quote 000"), and not just what i have called them (quote5) - have i done anything majorly wrong, or am i missing something small?

A little more info:

Thanks a lot, any help would be great!

Upvotes: 0

Views: 4300

Answers (4)

Rachel Harvey
Rachel Harvey

Reputation: 1789

Well, you're just creating a string object and putting it in the label. Also, you want to be referencing a variable, but you're doing it before you declare the variables. Your range is also not quite right, your random number will yield something between 1-5, but you want 0-6. You could put a 7 in and take away the +1, but I would do it differently.

I would personally put them all in an array and do it like this:

@IBAction func quoteButtonTapped(sender: AnyObject) {

    let quotes: NSArray = ["Quote 000", "Quote 001", "Quote 002", "Quote 003", "Quote 004", "Quote 005", "Quote 006"]

    let range: UInt32 = UInt32(quotes.count)

    let randomNumber = Int(arc4random_uniform(range))
    let QuoteString = quotes.objectAtIndex(randomNumber)

    self.quoteLabel.text = QuoteString as? String

}

Upvotes: 0

Duncan C
Duncan C

Reputation: 131481

Don't create a bunch of separate quote variables. Create an array of quotes:

let quotes = [
  "Now is the time for all good men to come to the aid of their country",
  "Vini, vidi, vichi",
  "I regret that I have but one life to give for my country",
  "Illegitimi non carborundum"
  ]

let index = arc4random_uniform(UInt32(quotes.count))
let aQuote = quotes[index]

By the way, don't post images of code. Use Code tags to put the code into your post, like I did above.

Upvotes: 4

simons
simons

Reputation: 2400

How do i get the Label to display the variables ("Quote 000"), and not just what i have called them (quote5) - have i done anything majorly wrong, or am i missing something small?

The problem is that this line of code var QuoteString: String = String(format: "Quote 00%d", randomNumber) is literally assigning a String value to QuoteString. It is not referencing your variables quote0 .. quote6 and their associated quotes. You are literally telling it that QuoteString = the bit in "" + the random number.

These 2 lines I think do what you you are trying to achieve.

    let quoteArray = ["Quote 000", "Quote 001", "Quote 002", "Quote 003", "Quote 004", "Quote 005", "Quote 006"]
    self.quoteLabel.text = quoteArray[Int(arc4random_uniform(UInt32(quoteArray.count)))]

Upvotes: 2

Asdrubal
Asdrubal

Reputation: 2481

Since you are just using a string anyways you don't need to have all the string vars that's extra code. Just use:

var QuoteString:String = String(format:Quote 00%d,randomNumber)

or

var QuoteString:String = String(format:Quote 00\(randomNumber))

you shouldn't use a string like that to choose a variable name. The other option is to use a Switch Case but the above code is by far the best option.

Upvotes: 1

Related Questions