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 trialChris Wade
9,574 PointsXcode keeps asking to use let not var
When in the FactBook file and i try running the code to call the random facts Xcode is telling me that i should try using let instead of var. Its not giving me an error just a warning, i can still build the app without crashing but it just gives me one fact and thats it. Im not sure what I'm doing wrong here as my code seems to look identical to the one shown in the video. My code is below if anyone could help that would be awesome!
import Foundation
struct FactBook {
let factsArray = [
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of 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 almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built."]
func randomFact() -> String {
var unsignedArrayCount = UInt32(factsArray.count)
var unsignRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignRandomNumber)
}
}
14 Answers
agreatdaytocode
24,757 PointsChris,
Can you check your ViewController file and make sure you have the following:
@IBAction func showFunFact() {
funFactLabel.text = factBook.randomFact()
}
Also check to make sure the IBAction is connected to your button.
agreatdaytocode
24,757 PointsChris,
On what line are you getting this warning? If Xcode detects that one of your var's will never change it will suggest you use a let.
Chris Wade
9,574 PointsIts on every line after the func randomFact string. Iv tried it a few more times and it's still not working for me.
agreatdaytocode
24,757 PointsWhat's not working the app?
Chris Wade
9,574 PointsThe app will build but it will only show me one fact, the code I have put in to make it randomised is not working.
agreatdaytocode
24,757 PointsIf you post the code on github, I'll be happy to take a look at it. You can also compare it to the finished project.
Chris Wade
9,574 PointsI just had a look at github and i have found that my code matches everyones perfectly and i have even cut and pasted it into Xcode to see if it will work and still i get the little yellow triangle next to all the var statements. I have put the error below if this helps, thanks again.
Variable 'unsignedArrayCount' was never mutated; consider changing to 'let' constant
Chris Wade
9,574 PointsIt worked! thanks for all the help Aaron.
Michael Van Dusen
12,136 PointsGetting same error. Dont see the solution above nor in Aaron Ackermans github.
agreatdaytocode
24,757 PointsWhich error message Michael, we have a few listed above.
Michael Van Dusen
12,136 PointsThese three all get 'Let' warning var unsignedArrayCount = UInt32(factsArray.count) var unsignRandomNumber = arc4random_uniform(unsignedArrayCount) var randomNumber = Int(unsignRandomNumber)
agreatdaytocode
24,757 PointsWhat happens when you change them to a var?
Michael Van Dusen
12,136 Pointsand I already check the view finder suggestion above.
Michael Van Dusen
12,136 PointsThey are set as var it tells me they should be "let" but that doesnt work either. My error is identical to the OP's post!
agreatdaytocode
24,757 PointsWhen you change them to lets and run the app what happens?
Rick Matsumoto
2,547 PointsIf we could get back to the original post, I too got the warnings that the variable "was never mutated" on all three variables. However, after compiling the code it did run successfully, giving me random facts after every button push. When I change the three variables to constants, the warning messages go away and the code still successfully compiles, and I still get random facts after every button push. Why didn't the instructor address this in the video? I won't lose any sleep over it since it works either way, but it's a little disconcerting to have to either ignore the instructor's code (using var) or ignore Xcode's warning (to use let).
Michael Van Dusen
12,136 PointsThe instructor didn't address it in this video because at the time of the shooting what he was explaining was accurate. Changes in the new version of Xcode break much of the code that is explained in these videos (ie. print vs println) that is why Pasan Premaratne has been working hard to bring us a new and updated Swift Course. I recommend you check that out!
Michael Van Dusen
12,136 PointsAGHHHH! It works that's what happens...after you reset this POS sorry excuse for a computer. Apparently the problem was something on my wife's Macbook Air froze, the simulator was remaining blank so I thought the code was broken. I have been using PC's since I was thirteen and being forced to use this crApple device is about to kill me!
agreatdaytocode
24,757 PointsHey now! that's a win in my book good to know it works :)
Michael Van Dusen
12,136 PointsI'll take it! I appreciate your patience with me...but I am afraid that my significant hatred for anything produced by crApple is going to make it impossible for me to complete this course.
agreatdaytocode
24,757 PointsLOL, give it time it will grow on you. You know you have crossed over when you no longer worry about what specs your iPhone or laptop have. When you go to buy the next upgrade you don't question it..when your friends ask what the specs are on the new device you reply who cares.. at that point your and apple fan boy like myself lol.
Michael Van Dusen
12,136 PointsI almost bought a mac mini the other day. I had been having panic attacks for a week leading up to it because I felt like buying said device would be tantamount to cheating on my wife. Then when I went to the the website to choose a model and noticed the specs I just about had an aneurysm! I just built a new Windows machine Skylake i7-6700k 16GB ram Samsung SSD's, GTX 980ti GPU etc etc. I got all of that for under $2k and it is a dream of a machine. I was going to have to pay half that and get a DUAL CORE PROCESSOR??? After my wife confirmed that I wasnt dreaming and that I hadnt slipped into a time machine and traveled back to 2005 I did some more research and found out that the 2012 Mac Mini that a user could add their own RAM and HD to and offered a Quad Core i7 had been downgraded to its current laughable condition with soldered in RAM and a nearly unremovable HD. That move beautifully illustrates why I will never purchase anything from that company!
agreatdaytocode
24,757 PointsThe spec delta has always been large between windows and Mac. But I have no regrets, it does what I need it to do and they last for a long time. I still have my first iMac (6 years old) its running El Capitan.. I will admit its going to need a new hard drive soon.
chaz
25,372 PointsThis is entirely irrelevant to the post, and dilutes the information people are looking for here. If you feel so strongly about something so meaningless, I suggest Reddit, rather than an educational forum.
agreatdaytocode
24,757 Points@chazgiese Thank you for adding value to this post chain.
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsAwesome! Happy to help