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 Improving Our User Interface Introduction to Auto Layout

Ryan Maneo
Ryan Maneo
4,342 Points

How to get an incremented value version of a default initiation?

I am trying to rather than getting a random number to generate the facts, increment a base fact number so I can get the nextFact, that way they don't repeat.

 func getNextFact() -> String {
    let currentFactID: Int = 0
        var nextFact: Int = currentFactID++

        return facts[nextFact]
}
}

I just can't get it to work... I don't know why though.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I have a guess here, although I can't say for sure if it's correct or not. You've written:

var nextFact: Int = currentFactID++

Try changing this to:

var nextFact: Int = currentFactID += 1

It's my understanding that the increment operator ++ was scheduled for deprecation in Swift 3. It could be that you're running Swift 3 and it doesn't support that anymore.

Ryan Maneo
Ryan Maneo
4,342 Points

I got that error too, and tried it, still no. Thanks for your answer though :) I hope I can figure this out...

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

It seems like in this point in the course, your facts array should be present in a struct that's in another file and it's named factModel. If this is the case, then the return line should look like this:

return factModel.facts[nextFact]
Ryan Maneo
Ryan Maneo
4,342 Points

https://i.imgur.com/c4PpxvH.png yep :/ Also, I finished the base FunFacts App, I am simply modifying it now.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Also, your method of moving to the next fact by incrementing the index will eventually cause a runtime error. You might consider looping through the facts by incrementing the index and taking the modulus of the length of the facts array. That way when you get to the last fact and you press the button for a new fact it will go back to the first one.

Ryan Maneo
Ryan Maneo
4,342 Points

It won't let me declare a for-in loop inside of a structure. It returns "Decloration Expected" Despite there already being one... :/