Swift 4 - Useful Information - The Basics
Swift is ready for your next iOS and OSX project - or for addition into your current app - because Swift code works side-by-side with Objective-C.
We will be covering Swift version 4 in this course.
Use of the semicolon (;) following each statement is fully optional in Swift, unless you wish to place several independent statements on a single line, in which case the semi-colon is required.
Multiline comments in Swift can be nested within other multiline comments.
// this is a comment
/* this is also a comment,
but written over multiple lines */
You can run, save, and share your Swift codes on our Code Playground, without installing any additional software.
Constants and Variables
Multiple variables can be declared on a single line, separated with commas.let x = 0.0, y = 0.0, z = 0.0
The value of a constant can never be changed. Trying to assign a new value to a constant results in an error.
Constant and variable names can contain almost any character, including Unicode characters:
However, constant and variable names must have no blank spaces, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line and box-drawing characters. Numbers can appear anywhere within a name, except for at the beginning.
Constants or variables of a certain type can't be declared again with the same name, nor can they be altered to store values of differing types. Also, a constant cannot be made a variable, nor can a variable be made a constant.
Type Annotations
Swift's basic types include:
Int: Integers
Double and Float: Floating-Point Values
Bool: Boolean Values
String: Textual Data.
It's possible to define multiple related variables of the same type. Include them all on a single line, separated by commas, then add a single type annotation after the final variable name:
var red, green, blue: Double
var red, green, blue: Double
Swift always chooses Double (as opposed to Float) when inferring the type for floating-point numbers.
Swift is a type safe language, meaning that it supports clarity when specifying value types for code. When part of your code expects a String, you can't pass it an Int by mistake.
Ternary Conditional Operator
The example above will evaluate the expression gender == 0. If true, it prints "male". Otherwise, it prints "female".
Range Operators
Swift offers two range operators, which are shortcuts for expressing a range of values.
The closed range operator (a...b) defines a range running from a to b, and includes the values aand b. The value of a must not be greater than that of b.
The half-open range operator (a..<b) defines a range that runs from a to b, but does not include b. It is said to be half-open because it contains its first value, but not its final value. As with the closed range operator, the value of a must not be greater than that of b.
Logical Operators
Multiple logical operators can be combined to create longer compound expressions.
The logical operators && and || are left-associative, meaning that compound expressions with multiple logical operators evaluate the leftmost subexpression before the right.
Optionals
Optionals are used in situations in which a value may be absent.
An optional says:
-There is a value, and it equals x
or
-There isn't a value at all
An optional Int is written as Int?, not Int. The question mark indicates that the value contained within is optional, meaning that it might contain some Int value, or it might contain no value at all.
It can't contain anything else, such as a Bool value or a String value. It's either an Int, or it's nothing at all.
nil
You set an optional variable to a valueless state by assigning it the special value nil:An optional variable with no default value is automatically set to nil for you:
nil cannot be used with non-optional constants and variables. If your code contains a constant or variable that needs to work with the absence of a value under certain conditions, always declare it as an optional value of the appropriate type.