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 Build a Simple iPhone App with Swift 2.0 Structs as Data Models Structs or Classes

Not displaying Random Fact only displaying second Fact.

Hi when i run the app it is not displaying the facts but only the second. What could be the problem in this. thanks

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var funFactLabel: UILabel!
let factModel = FactModel()

override func viewDidLoad() {
    super.viewDidLoad()
    funFactLabel.text = factModel.facts[1]

    // Do any additional setup after loading the view, typically from a nib.
}

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

@IBAction func showFunFact(sender: AnyObject) {
    funFactLabel.text = factModel.facts[1]


}

}

Oliver Duncan
Oliver Duncan
16,642 Points

You should provide some of your code so we can see what might be going wrong.

Yes here you go import UIKit

class ViewController: UIViewController {

@IBOutlet weak var funFactLabel: UILabel!
let factModel = FactModel()

override func viewDidLoad() {
    super.viewDidLoad()
    funFactLabel.text = factModel.facts[1]

    // Do any additional setup after loading the view, typically from a nib.
}

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

@IBAction func showFunFact(sender: AnyObject) {
    funFactLabel.text = factModel.facts[1]


}

}

There is the view Controller. is there any other code you need.

1 Answer

Oliver Duncan
Oliver Duncan
16,642 Points

No that ought to do it thanks. In your viewDidLoad function, you are displaying factModel.facts[1], which is the second fact. Then in your button action showFunFact, you are displaying the same factModel.facts[1]. So when you run your app and press the button, nothing happens. Remember, arrays in Swift (and every programming language I've ever seen) start counting with 0, so factModel.facts[0] is actually the first fact in the facts array. Change your viewDidLoad to reflect this, and you'll press the button and see the facts change from the first item in the array to the second.

Displaying a random fact is covered in a lesson down the line, and you'll learn how to do it soon. It involves using another framework, UIGameKit, to generate a random number that you can use as an index of the facts array, thus accessing a random fact. Hope this helps.

Thanks