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 Constants

What is a "String"? The video showed the word after a colon but did not really explain what one is or why you use it.

var str : String = "Hello"

Why would one put a colon and the word "String" in this fashion? It talked about implicit or explicit variable type but this is my first-ever programming lesson and I have no idea what this means and need an example of when you would use it.

3 Answers

Daniel Johnson
Daniel Johnson
104,132 Points

A string is made up of one or more characters. It's basically the stuff between the quotes. This can include letters, numbers, symbols, etc.. Just think of it as the text you normally write.

Here's an example of a explicitly declared string. You are explicitly telling Swift that this is a string.

var explicit:String = "Hello"

However, this is not really needed because Swift can already figure out this variable is a string. If you already have a value to give your variable you could instead just declare the string implicitly.

var implicit = "Hello"

Another example of when you should explicitly type a string is if you wanted to declare a variable, but did not want to give it a value just yet. In certain coding situations you might need to do this.

var nothingYet:String

And then later on in your code you can just give it a value like this.

nothingYet = "Hello"

And then you could use that string however you want.

println(nothingYet)
Gregory Serfaty
Gregory Serfaty
37,140 Points

It is just to define explicitly the type of your variable.

Mike Atkinson
Mike Atkinson
6,882 Points

Great answer Daniel Johnson!

Yes, to put it simply:

  • "It is the the stuff between the quotes."
  • It one of the many types of ways to store information in programming. You can think of it as a type of container.
  • It is a stored set of charters.

eg.

1234 --> not a string, it's a value.

"Don't worry, but happy" --> A string, letters and characters stored in a sequence.

"1234" --> a string, characters that happen to be numbers stored in a sequence.