importCocoa// "var" means make a new variablevargreeting="Hello, playground"print(greeting)varname="hello"print(name)name="world"print(name)// let: Declares a constant that cannot be changed once it has been set, i.e., an immutable variableletlang="python"print(lang)// lang = "java" // Error: cannot assign to value// print(lang)/*
Best practice suggests that you should default to using let to define a variable and only use var when you explicitly need the variable’s value to change. This helps lead to cleaner, more predictable code.
*/// ## Create stringsletquote="Then he tapped a sign \"Believe\" and walked away"// multiple line stringletmovie="""
hello
世界
"""print(movie)print(movie.count)// 8 -> count returnes unicode countlettrimmed=movie.trimmingCharacters(in:.whitespacesAndNewlines)print(trimmed.count)// define integersletscore=10print(score/3)// 3varpoints=10points+=1print(points)// define floating numberletnumber=0.1+0.2print(number)leta=1letb=2.0// int and double cannot sum, must cast firstletc=Double(a)+bletd=a+Int(b)
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)