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 trialMorgan Li
1,277 PointsWhy do you type out "var status: String" in this question? You have not assigned anything to it at the beginning.
This is the quiz question 3 for me.
2 Answers
Heidi Puk Hermann
33,366 PointsIn Swift you have to assign the type to the variable/constant when you create it. This is because Swift is a type-safe language meaning that once you have defined a variable/constant and given it a type, the type cannot be changed.
var emptyString1: String //1
var emptyString2: String = "" //2
var emptyString3 = "" //3
//1 - If you don't want to or can't assign a value when you define your variable, you just give the type and leaves it empty. //2 and //3 - These two methods are essentially the same, since we give it a value of an empty string. In the latter the explicit definition of the type has just been omitted.
Jhoan Arango
14,575 PointsHello Morgan,
I'm not sure where you are stuck with this question, but "var status: String" will have a value once the conditional statement has done all the checks.
var isAvailable = true
var isCheap = true
var status: String
if !isCheap && !isAvailable {
status = "super rare"
} else if isCheap && isAvailable {
status = "not rare"
// status will have a "not rare" value after evaluating all the conditions.
} else if !isCheap && isAvailable {
status = "somewhat rare"
} else {
status = "common"
}
Good luck
Morgan Li
1,277 PointsMorgan Li
1,277 PointsThanks, your explanation to //1 was what i was looking for. I didn’t know that you can define a variable but not assign anything to it.