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 trialRyan Maneo
4,342 PointsHow 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
Treehouse TeacherI 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.
Jennifer Nordell
Treehouse TeacherIt 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
4,342 Pointshttps://i.imgur.com/c4PpxvH.png yep :/ Also, I finished the base FunFacts App, I am simply modifying it now.
Jennifer Nordell
Treehouse TeacherAlso, 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
4,342 PointsIt won't let me declare a for-in loop inside of a structure. It returns "Decloration Expected" Despite there already being one... :/
Ryan Maneo
4,342 PointsRyan Maneo
4,342 PointsI got that error too, and tried it, still no. Thanks for your answer though :) I hope I can figure this out...