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
Avith Gutierrez
2,797 PointsSwift Background using random pictures?
Is it possible instead of having a color wheel in the fun fact app, to have several images displaying as a random background?
Thanks.
2 Answers
Kyle Pontius
6,190 PointsAbsolutely! In the weather app that you're going to be building next they will have you take an input string and pull an image that matches that name. The imageName = "someName" that you see below is the name of the image file itself in the Images.xcassets folder. I've pasted the method (from the weather app) below that takes the input string and returns a UIImage. This will help you see how to access UIImages.
The way you would then make this random is by taking the colorsArray[], from the Fun Facts app, that returns random colors, and storing UIImage's instead of colors (See link below). The randomColor() -> UIColor will need to be changed to return UIImage rather than UIColor.
I've intentionally left some room there for your to figure all this out, you definitely can do this though. Some other helpful references are: http://stackoverflow.com/questions/24049537/defining-the-file-a-uiimage-will-use-swift http://stackoverflow.com/questions/24003191/pick-a-random-element-from-an-array http://stackoverflow.com/questions/24172180/swift-creating-an-array-of-uiimage
func weatherIconFromString(stringIcon: String) -> UIImage {
var imageName: String
switch stringIcon {
case "clear-day":
imageName = "clear-day"
case "clear-night":
imageName = "clear-night"
default:
imageName = "default"
}
var iconName = UIImage(named: imageName)
return iconName!
}
Avith Gutierrez
2,797 PointsHello Kyle, perfect so I was going some steps ahead, i will take the final lessons for the stormy app, also do you know how can i use the struct with random but they do not repeat ?
struct FactBook {
// This will hold the random index
var previousRandomIndex = 0
let factsArray = [
"Los Pandas son muy bonitos.",
"Pandis, están muy contentos de que eres su papi.",
"Lo mejor del mundo panda es ser peludos.",
"El 21 de Marzo es el Pandaversario.",
"Toma menos de 5 minutos comernos un cupcake, jijiji.",
"Los pandas, hacen muy buen equipo.",
"Vamos a ser pandas famosos.",
"A Panda Baby Bear le gusta ir a Cancún.",
"Panda Baby Bear es muy bebeshito.",
"Es saludable darle masajes a patita de Panda Baby Bear.",
"Estudios demuestran que los pandas son muy felices.",
"El cariño es la forma más sana de consentir a un panda.",
"Un panda prefiere más veces un bombón que una paleta.",
"Se dice que los pandas se acurrucan muy fácil.",
"Si un panda te abraza, Felicidades eres muy afortunado.",
"El 27 de Junio es el día del Panda Baby Bear.",
"Los Pandas gustán de comer pan de elote.",
"A veces extrañamos Coffee Bean.",
"Alegría es compartir una bonita tarde acurrucados.",
]
func randomFact() -> String {
var unsignedArrayCount = UInt32(factsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
do {
randomNumber = Int(unsignedRandomNumber)
} while randomNumber == previousRandomIndex
return factsArray[randomNumber]
}
}
This is from the FunFactsApp it actually works, but at the time i reached the total numbers of the array, the app stops working.
Should i change something in the UInt32()
Where can i follow you?
Thanks.