Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Hi im having trouble with this swift code, it buils succesfully but once i press the button nothing displays

Its a dice game and i included the dice refrence in the array called allOf and i want to display diffrent dices afer each click. heres the code thanks.

import Foundation
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var firstDice: UIImageView!
    @IBOutlet weak var secondDice: UIImageView!
    @IBOutlet weak var playIt: UIButton!

 var allOf = ["Dice 1", "Dice 2", "Dice 3", "Dice 4", "Dice 5","Dice 6"]

    override func viewDidLoad() {
        super.viewDidLoad()

        func randomDice()-> String{

        var unsignedArrray = UInt32 (allOf.count)
        var randomNumber = arc4random_uniform(unsignedArrray)
        var riceKicks = Int (randomNumber)

        return allOf[riceKicks]
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func playBtn(sender: UIButton) {
        self.firstDice.image = UIImage(named: "allof")
        self.secondDice.image = UIImage(named: "allOf")

    }


}

2 Answers

Hello:

Try this :

import Foundation
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var firstDice: UIImageView!
    @IBOutlet weak var secondDice: UIImageView!
    @IBOutlet weak var playIt: UIButton!

 var allOf = ["Dice 1", "Dice 2", "Dice 3", "Dice 4", "Dice 5","Dice 6"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Call randomDice when the view loads
        randomDice()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func playBtn(sender: UIButton) {
        self.firstDice.image = UIImage(named: "allof")
        self.secondDice.image = UIImage(named: "allOf")
        // Call randomDice function when you press the button.
        randomDice()
    }

        func randomDice()-> String{

        var unsignedArrray = UInt32 (allOf.count)
        var randomNumber = arc4random_uniform(unsignedArrray)
        var riceKicks = Int (randomNumber)

        return allOf[riceKicks]
        }
}

See how that works. Hope this solves your problem.

hey dude thanks

Here's the new code and theres is still no error but the image still doesn't show :/

import Foundation
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var firstDice: UIImageView!
    @IBOutlet weak var secondDice: UIImageView!
    @IBOutlet weak var playDice: UIButton!

    var allOf: [UIImage] = [

        UIImage(named: "Dice 1")!,
        UIImage(named: "Dice 2")!,
        UIImage(named: "Dice 3")!,
        UIImage(named: "Dice 4")!,
        UIImage(named: "Dice 5")!,
        UIImage(named: "Dice 6")!
        ]


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        randomDice()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }

    @IBAction func playGame(sender: AnyObject) {

       self.firstDice.image = UIImage(named: "allOf")
        self.secondDice.image = UIImage (named: "allOf")

        randomDice()

    }
    func randomDice()->UIImage{
        var unsignedArrray = UInt32 (allOf.count)
        var randomNumber = arc4random_uniform(unsignedArrray)
        var riceKicks = Int (randomNumber)

        return allOf[riceKicks]

    }

}

Ok so what is happening is that when you press the button "playGame", it's setting the pictures, and then nothing else is happening, because you are not using your random function in the array.

So here are the latest changes.

import Foundation
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var firstDice: UIImageView!
    @IBOutlet weak var secondDice: UIImageView!
    @IBOutlet weak var playDice: UIButton!

    var allOf: [UIImage] = [
        UIImage(named: "Dice 1")!,
        UIImage(named: "Dice 2")!,
        UIImage(named: "Dice 3")!,
        UIImage(named: "Dice 4")!,
        UIImage(named: "Dice 5")!,
        UIImage(named: "Dice 6")!
    ]


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        randomDice()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

   // Deleted the hard coding on the dices. 
    @IBAction func playGame(sender: AnyObject) {
        randomDice()
    }

  // Deleted the UIImage return, we don't need it anymore.  
    func randomDice() {
        let unsignedArrray = UInt32 (allOf.count)
        let randomNumber = arc4random_uniform(unsignedArrray)
        let riceKicks = Int (randomNumber)

        self.firstDice.image = allOf[riceKicks]
        self.secondDice.image = allOf[riceKicks]
    }
}

This should do the trick, let me know.