"Debugging for iOS" was retired on May 31, 2020.

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

Create a constant and variable

How come it wont work for me,

i try and register my var and str and it never works even when i copy word for word

Can you post you code?

var str : String = "Hello "

let modernProgramminglanguage: String = "Swift"

var greeting = str + modernProgramminglanguage // implicit type or inferred type

2 Answers

Not sure exactly what you are asking or what the issue is, but this is explicit typing:

var str : String = "Hello "

and this is implicit typing:

var greeting = str + modernProgramminglanguage

What that means is that swift can figure out that greeting is a String given that you are assigning a String to it. If it were explicit it would be:

var greeting: String = str + modernProgramminglanguage

But I get the feeling that you are having a problem with a challenge. If you don't say what the challenge wants, or better, give a link to the challenge, then we're left guessing what the issue is.

P.S., re nomenclature. The standard lingo is to declare variables, e.g.,

var greeting: String  //creates the variable, type is required

and to initialize them (give them a value):

greeting = "hello"  //gives it a value

Declaration and initialization can be done in one step:

var greeting = "hello"  //creates and gives it a value in one step, type not needed as swift can infer from "hello"

Ok, I don't know which challenge you're referring to but I'm guessing you're supposed to use string interpolation. instead of adding the strings together you should do:

var greeting = "\(str) \(modernProgramminglanguage)"

string interpolation allows you to use variables inside a string instead of having to adding them together