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 Swift Basics (retired) Types Numbers

I think the question for task #2 is incorrect, it is asking for a constant when it should be asking for a variable. Yes?

The request is to create a constant - but it is for price = 9.99 which should be a var

numbers.swift
let title = "A Dance with Dragons"
var price = 9.99 // double 

2 Answers

Stone Preston
Stone Preston
42,016 Points

task 2 states: Next create a constant called price and assign it the value 9.99.

you define constants using the let keyword

let someConstant = someValue

so you would use:

let title = "A Dance with Dragons"
let price = 9.99

Constant values cannot change after they are set. using a constant in this instance is correct if we dont want the price to ever change. In this case the we want the price of A Dance With Dragons to always be 9.99, so it makes sense to use a constant

It does not matter that price is a double. any data type can be a constant and any data type can be a var. The difference is that the values of vars can change, and the value of constants cannot.

for more information on constants and variables see the Swift eBook which explains the main difference between constants and variables:

Constants and variables associate a name (such as maximumNumberOfLoginAttempts or welcomeMessage) with a value of a particular type (such as the number 10 or the string "Hello"). The value of a constant cannot be changed once it is set, whereas a variable can be set to a different value in the future.

thank you! when I was checking the work, it stated it should be a "double" but //double wasn't letting y work get approved - I tried it in xCode and it seems fine - prob user error :)

Stone Preston
Stone Preston
42,016 Points

ah. well swift infers its type as double. you could have explicitly declared the type as double if you wanted:

let price: Double = 9.99

note that price is still declared as a constant, it just has an explicit type (as opposed to an inferred type)