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 Printing Results

Xcode - "Cannot assign to the result of this expression"?

var str = String = "Hello "

What's wrong with my code?

3 Answers

Joe Dayvie
Joe Dayvie
11,956 Points

Hello,

I am not sure where you are in the Swift training, but what you typed here should actually be:

var str: String = "Hello "

I hope this helps =)

I'm on 'Printing Results'.. Thank you so much! :)

Hi there

The '=' sign when used in programming, assigns the value on the right, to the left of the expression

We can say something like

var yourName = "Mustafa Kasim"

Here, the computer automatically understands that the variable 'yourName' should be of a string datatype, based on the value I assigned to it on the right

But, as the programmer, you can let the computer know that your value will definitely be a string by mentioning it in your code like

var yourName:String = "Mustafa Kasim"

By putting the ':String', you have explicitly told the computer that your variable 'yourName' will be a String datatype. And then, you assign it the value

When you did an "var str = String = "Hello". You had a typo before the datatype. And the computer doesn't know what '= String' should do, so it throws an error and doesn't execute. Change it to

var str : String = "Hello" 

That will ask a variable named 'str' of type 'String' to be created, and assigned a value of "Hello".

Best

Thank you so much!! What a thorough explanation. Sorry I couldn't give you a best answer, I already gave it to the above person.

But thank you so much for taking the time out to help me in this thoughtful way.

Maher Alhasan
Maher Alhasan
974 Points

var username = "Maher" str = username var Morning = "Hi " str = Morning + username print(Morning + username)