Swift Language Declarations
var — a mutable variable
let — an immutable constant
|Int||integers, recommended for most numbers|
|Float (32-bit and 64-but)|
|Float80 (80-but numbers)|
|Double (64 -bit numbers)|
|Array<____>||an array of whatever is inside the braces||• Strongly typed, so the type of elements it will contain must be declared along with the array itself. for example, to declare an array that will contain strings but start out as empty. short hand is|
|Dictionary<______>||• An unordered collection of key-value pairs. The keys can be of any type, but must be unique. The keys must be hashable, which allows the dictionary to guarantee they are unique. (Basic Swift types are hashable.)|
• The values can be of any type, including structures & classes.
|• Strongly typed can only contain keys and values of the declared type. |
|Set||• Similar to an array in that it contains elements of the same type, but unordered. |
• As well, the items in the set must also be unique and hashable.
• A “literal” is what happens when you type a string, a number, or a float directly into your code. It simply means the compiler interprets this “literally” to be just what it looks like.
• Shorthand for accessing arrays
To retrieve an element from an array, you provide the element’s index in square brackets after the array name.
• For arrays, if you attempt to access an out of bounds index results in a trap. A trip is a runtime error that is trapped by iOS before the program gets into an “unknown state.”
• For Dictionaries, accessing an element out of bounds returns an optional.
• Are indicated by adding
? to the end of a type name.
• Expresses the allowance that the variable may not contain any value at all.
• You cannot use optional floats like non-optional floats. You must first deal with the possibility of nil, which is called unwrapping.
• Optional binding is using an if-let statement to catch for the nil condition without causing the trap.
• Forced unwrapping is when you append
! to the optional name. However, if you forcibly unwrap an object that results in a nil, it will result in a trap.