a ?? b unwraps an optional
a if it contains a value, or returns a default value
a != nil ? a! : b
var someInts = [Int]()
Double has a precision of at least 15 decimal digits, whereas the precision of
Float can be as little as 6 decimal digits. The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate,
Double is preferred.
Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers.
Conversions between integer and floating-point numeric types must be made explicit:
Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. You write an in-out parameter by placing the inout keyword right before a parameter’s type. You place an ampersand (&) directly before a variable’s name when you pass it as an argument to an in-out parameter, to indicate that it can be modified by the function.
In-out parameters are an alternative way for a function to have an effect outside of the scope of its function body.
Structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods.
However, if you need to modify the properties of your structure or enumeration within a particular method, you can opt in to mutating behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends.
The method can also assign a completely new instance to its implicit self property, and this new instance will replace the existing one when the method ends.
Mutating methods for enumerations can set the implicit self parameter to be a different case from the same enumeration.
Use the type check operator
is to check whether an instance is of a certain subclass type. The type check operator returns
true if the instance is of that subclass type and
false if it is not.
You can use the is and as operators in a switch statement’s cases to discover the specific type of a constant or variable that is known only to be of type Any or AnyObject.
Use the conditional form of the type cast operator
as? when you are not sure if the downcast will succeed.
Use the forced form of the type cast operator
as! only when you are sure that the downcast will always succeed.