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) Variables and Constants Variables

stephen smith
stephen smith
455 Points

create a variable named greetings and assign it hello what am i doing wrong? var greetings = "hello"

need help on first problem of how to assign the variables.

string_assignment.swift
var greetings = "HELLO"

3 Answers

You have two options to correctly satisfy this code challenge, which highlight a very important concept in the swift programming language - type inference.

Answer method number 1:

var greeting : String = "Hello"

Here, a variable with the name 'greeting' is declared with the 'var' keyword, specifies that the value it will assign itself to is a String data type, then finally the variable 'greeting' uses the = assignment operator to assign, or store a reference to in memory, the string "Hello".

Answer method number 2:

var greeting = "Hello"

This variable declaration and assignment is interpreted by swift's compiler as exactly the same as the first answer. Swift is able to 'infer' that the variable "greeting" is of type String without us explicitly stating it's data type as String as in the first answer. It does this by simply looking at the value the variable greeting is assigned to, "Hello", sees that it is a string, then appropriately infers the greeting variable's type as String.

Tomas Pavlik
Tomas Pavlik
26,726 Points

Do not forget to end the statement with a semicolon ; T.

Wouter Hubers
Wouter Hubers
7,451 Points

You don't have to end the statement with a semicolon.

Try assigning 'hello' exactly as it been asked. So lose the CAPS. Let me know if it worked ;-)

stephen smith
stephen smith
455 Points

thanks i figured it out last night, i just had "Hello" written as "HELLO" and it worked.