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

Caleb Kleveter
Treehouse Moderator 37,862 PointsUIImage array in Swift.
I am trying to create an array of images, then randomly select one; this is my code so far:
import Foundation
struct RandomDiceNumbers {
let diceImages: [UIImage] = [
UIImage(named: "")!
]
func randomNumber() -> UIImage {
var unsignedArrayCount = UInt32(diceImages.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return diceImages[randomNumber]
}
}
Were I have the array type specified and the return type of the function specified as a UIImage I get a error saying: "Use on undeclared type 'UIImage'". I also get an error in the unsignedArrayCount variable at diceImages I get an error that says "'RandomDiceNumbers' does not have a member named 'diceImages'".
Is it because I don't have the image file path in the array yet?
4 Answers

Rasmus Rønholt
Courses Plus Student 17,358 PointsA couple of things...
- UIImage is part of UIKit, so you need to import that before you can make anything work
- I would consider naming otherwise, and creating the images in an initialiser - something like:
import Foundation
import UIKit
struct Dice {
var images:[UIImage]
init() {
//Initialise your images here
}
func randomImage() -> UIImage {
return images[Int(arc4random_uniform(UInt32(images.count)))]
}
}
let randomDiceImage = Dice().randomImage()

Marlon Henry
6,885 PointsWhat type of instance is diceImages? And if you trying to call "randomNumber" function on diceimages you might want to do diceImages.randomNumber() Also why are you naming var with the same name of your functions?

Caleb Kleveter
Treehouse Moderator 37,862 PointsRasmus, could you explain why I would initialize the images vs. sticking them in the variable? And why wold I keep the variable there?

Rasmus Rønholt
Courses Plus Student 17,358 PointsWell since you ask, I am forced to reflect on why… I guess it’s primarily because in init() you would have control over what happens if the image can’t load for som reason. But I guess in this case (where the images are all build in resources, I imagine) it doesn’t really matter. So in the end its a matter of preference - unless we are talking a lot of images and optimising perfomance.
But if we are really getting picky, then I would propose that the functionality might be better implemented with enum values. Something like this:
import Foundation
import UIKit
enum DiceValue: UInt32 {
case Black, White, Red, Green, Blue, Yellow
var imageName:String {
switch self {
case .Black:
return "black"
case .White:
return "white"
case .Red:
return "red"
case .Green:
return "green"
case .Blue:
return "blue"
case .Yellow:
return "yellow"
}
}
var image:UIImage {
return UIImage(named: self.imageName)!
}
}
struct Dice {
static let diceCount:UInt32 = 6
static func roll() -> DiceValue? {
return DiceValue(rawValue: arc4random_uniform(diceCount))
}
}
Then you can do stuff like:
let initialValue = DiceValue.Black
if let newRoll = Dice.roll() {
myImage = newRoll.image
}
And extending functionality with say points, or a different set of dice becomes easier. May have gone a bit overboard with this one, so gonna stop now :) Best of luck...

Caleb Kleveter
Treehouse Moderator 37,862 PointsOkay, Thanks! I imported the UIKit framework and all the issues disappeared!