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 trialMustafa Kasim
1,938 PointsXcode - "Cannot assign to the result of this expression"?
var str = String = "Hello "
What's wrong with my code?
3 Answers
Joe Dayvie
11,956 PointsHello,
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 =)
ankushdharkar
7,530 PointsHi 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
Mustafa Kasim
1,938 PointsThank 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
974 Pointsvar username = "Maher" str = username var Morning = "Hi " str = Morning + username print(Morning + username)
Mustafa Kasim
1,938 PointsMustafa Kasim
1,938 PointsI'm on 'Printing Results'.. Thank you so much! :)