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

mahi
mahi
7,137 Points

Just experimenting. Need some help.

after making a struct similar to the video, i wanted to experiment something. As am a bit familiar with FOR LOOP from prior experience, i used it in here to display all the values of the string one by one on each click of the button. But when I click the button, it jumps directly to the last value of the string.

import UIKit

class ViewController: UIViewController {
var factModel = FactModel()
@IBOutlet weak var funFactLabel: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    funFactLabel.text = factModel.facts[0]
}

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

@IBAction func showFunFact() {
    for fact in factModel.facts{
    funFactLabel.text = fact
    }
}
 }
mahi
mahi
7,137 Points

tried using C for loop swell, but isn't working

import UIKit

class ViewController: UIViewController {
        var factModel = FactModel()
@IBOutlet weak var funFactLabel: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    funFactLabel.text = factModel.facts[0]
}

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

@IBAction func showFunFact() {
    var i : Int
    for(i = 0; i<factModel.facts.count; i += 1){
        funFactLabel.text = factModel.facts[i]
    }
    }

}

1 Answer

Silviu B.
Silviu B.
3,111 Points

I think the problem is inside your showFunFact(). If you want to append the string, you might want to try inside your loop the following code:

funFactLabel.text.stringByAppendingString(fact)
mahi
mahi
7,137 Points

i got my answer from the next videos. Anyways thanks a lot!!