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 trialNadia Matin
1,976 PointsWhy is my app not running?
My code is not working, I have no idea why and I'd greatly appreciate if someone could help me: View Controller-
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var funFactLabel: UILabel!
let factBook = FactBook()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
funFactLabel.text = factBook.randomFact()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
funFactLabel.text = factBook.randomFact()
}
}
factBook:
import Foundation
struct FactBook {
let factsArray = [
"Ants strech when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold metals are actually made of mostly silver.",
"You are born with 300 bones; by the time you are an adult, you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow up to a meter in a day.",
"Some penguins can leap 2-3 meters out of the water.",
"The state of Florida is bigger than England.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being build."
]
func randomFact() -> String {
let unsignedArrayCount = UInt32(factsArray.count)
let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
let randomNumber = Int(unsignedRandomNumber)
return factsArray[randomNumber]
}
}
4 Answers
Vanessa Cantero Gómez
7,376 PointsHi Nadia, can you paste here the console output? And also, what do you mean with not working? Can you explain more?
Nadia Matin
1,976 PointsHi Vanessa, my console output is this:
self FunFacts.FactBook
unsignedArrayCount UInt32 24 24
unsignedRandomNumber UInt32 69314379 69314379
randomNumber Int 140734730484240 140734730484240
By not working I mean xCode says the build is successful then does not display the build on the simulator and does not give me specific errors.
Vanessa Cantero Gómez
7,376 PointsI just created a project with this code and it works fine. I have no idea what you are missing. Do you mind to upload your code somewhere so I can help you?
Nadia Matin
1,976 PointsI just closed xCode, and reopened it. When I ran the code again, it worked fine. Sorry to bother you, I honestly don't know what was going wrong earlier, but it's working now. Thank you so much for your help though!
Vanessa Cantero Gómez
7,376 PointsGo home Xcode, you're drunk.
No worries, happy to help =).