Use let to declare constants, and use Var to declare variables.
The compiler automatically infers the type without having to explicitly declare the type and assign the value at the same time as the declaration. If the initial value does not provide enough information (or no initial value), it is necessary to declare its type after the variable name, separated by a colon.
var myvariable = 42
myvariable = 50
Let myconstant = 42
Let Implicitinteger = 70
Let implicitdouble = 70.0
Let explicitdouble:double = 70.0//explicit specified constant type
Values are never implicitly converted to other types. If you need to convert a value to a different type, explicitly convert it.
Let label = "the width is"
Let width = 94
Let Widthlabel = label + String (width)
There is a simpler way to convert a value to a string: write the value in parentheses, and write a backslash in front of the parentheses.
Let apples = 3
Let oranges = 4
Let applesummary = "I has \ (apples) apples."
Let fruitsummary = "I had \ (apples + oranges) pieces of fruit."
Use square brackets [] to create arrays and dictionaries, and use subscripts or keys to access elements. A comma is allowed after the last element.
var shoppinglist = ["Catfish", "water", "tulips", "Blue paint"]
SHOPPINGLIST[1] = "Bottle of Water"
var occupations = [
"Malcolm": "Captain",
"Keylee": "Machanic",
]
occupations["Jayne"] = "Public Relations"
To create an empty array or dictionary, use the initialization syntax.
Let Emptyarray = [String] ()
Let emptydictionary = [String:float] ()
If the type information can be inferred, you can use [] and [:] To create an empty array and an empty dictionary-just as you would declare a variable or pass arguments to a function.
Shoppinglist = []
Occupations = [:]
END
Swift Basic Learning (four) simple values