2

I am trying to get a random string from array "firstArray" and print it in UILabel "label". I cannot seem to figure it out and I get errors. Your help is appreciated. I tried searching but could not find any up-to-date tutorials/methods.

import UIKit

class ViewController: UIViewController {
    @IBOutlet var label: UILabel!

    @IBAction func random(_ sender: Any) {
        let firstArray = [ "hi" , "bye" , "hello"]
    }
Desdenova
  • 5,326
  • 8
  • 37
  • 45
R. Khaira
  • 131
  • 1
  • 9
  • 1
    Update your question with the code causing the error and post the error message. – rmaddy Dec 21 '16 at 06:29
  • your question is not a really bad question, so i won't downvote it. But you really need to post more info, like how you implement the 'random' function. (cause i do not see any random algo. from you code, you just defined a array of string) – Pang Ho Ming Dec 21 '16 at 06:31
  • analyses the code before post for proper basic opening and closing brackets at least. – vaibhav Dec 21 '16 at 06:33
  • 1
    This might be what you are looking for: [Pick a random element from an array](http://stackoverflow.com/questions/24003191/pick-a-random-element-from-an-array). – Martin R Dec 21 '16 at 06:34
  • One tip to improve your code is, don't name your `IBAction` method `random`, you see people don't even know where you want to implement the random function. Write another function called `random`, then put it inside that `IBAction`, it'll be easier for you to read later on :) – Bright Dec 21 '16 at 06:39

2 Answers2

3

I'd rather using arc4random(), this code will pick up random items from your array:

let firstArray = ["hi", "bye", "hello"]
    let randomItem = Int(arc4random() % UInt32(firstArray.count))
    myLabel.text = "\(firstArray[randomItem])"
Frank Eno
  • 2,581
  • 2
  • 31
  • 54
  • 1
    This makes no attempt to get a random value from the array and it assigns the random `Int` to the label. – rmaddy Dec 21 '16 at 06:53
  • yep, mistyped error, i've forgot the firstArray[] before \(randomItem) :) – Frank Eno Dec 21 '16 at 07:08
  • 1
    Please avoid posting code-only answers. Try to explain how your code solves the OP's problem, and help others who are less experienced understand your solution. – ad absurdum Dec 21 '16 at 08:10
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Dec 21 '16 at 08:23
  • @fvimagination its not working in swift 4 would you provide an update? – Xcodian Solangi Nov 08 '17 at 10:34
1

You have to generate random numbers between 0 and your array count, then set the label text to the corresponding item in array, like this:

@IBAction func random(_ sender: UIButton) {
     let firstArray = [ "hi" , "bye" , "hello"]
     var randomNumber = Int(arc4random_uniform(UInt32(firstArray.count)))
     randLabel.text = firstArray[randomNumber]
}

this thread is helpful to understand the random logic.

Community
  • 1
  • 1
Mina
  • 2,167
  • 2
  • 25
  • 32