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

Jose Gutierrez
Jose Gutierrez
170 Points

Basic String Question

What is the purpose of ": string =" when coding in Swift. It seems the code does the same thing with or without it?

2 Answers

Timothy Tuite
Timothy Tuite
5,429 Points

By putting : String you are specifing the data type as you get further into the course it will make more sense. Also It makes your code easier for others to read.

Dennis Parussini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Parussini
Treehouse Project Reviewer

Actually you can use the :Swift, :Int, :Double but you don't have to. If you initialize the var or let with a value, you don't need to also write :String. Example:

var gretting = "Hello"

If you don't know or don't want to give the var or let a value you HAVE to initialize it with the :String etc. Example:

var greeting: String