Swift is a compiled, multi-paradigm language. With the following goals:
Have safety features to prevent errors in production
Having comparable speed to C-based languages
Having syntax that is a joy to read and write
Swift was developed as a replacement for Objective-C, which was the primary language used for developing iOS and macOS applications. Swift was developed by Apple Inc. The language got its first release in 2014. It is now open source and is available on all Apple devices, Linux, and Windows.
Values can be associated with names by defining a variable and assigning a value to that variable. That name may then be used to refer to that value throughout the program. Variables are mutable, which means that the value can be changed at any time.
Variables are defined using the var
keyword.
var variableName = 10
Swift is a type-safe, statically typed language, which means all values have a type at compile time. You can either explicitly specify the type of a variable with type annotations or let the compiler infer the type.
var explicitVar: Int = 10 // Explicitly typed
var implicitVar = 10 // Implicitly typed
Updating a variable's value is done using the =
operator.
The type of a variable, is fixed once it is initially defined.
variableName = 13 // update to new value
// compiler error when assigning a different type
variableName = "Hello, world!" // Cannot assign value of type 'String' to type 'Int'
Variables may be declared without assigning a value by specifying the name and type, but they may not be used before a value is assigned.
var someInt: Int
// This would trigger a compiler error
// print(someInt) // Variable 'someInt' used before being initialized
// Assign a value to the names
someInt = 169
print(someInt) // Prints '169'
Constants are similar to variables in that they are associated with a name and a value, but the value of a constant cannot be changed once it is initially assigned and is thereby immutable.
Constants are defined using the let
keyword.
let constantName = 10 // constant
let explicitConstant: Int = 10 // Explicitly typed constant
Swift has operators for addition, subtraction, and multiplication.
The operators are +
, -
, and *
respectively.
print(1 + 1) // Prints 2
print(2 - 1) // Prints 1
print(2 * 2) // Prints 4
In Swift, functions are a chunk of code that performs a task.
A function has a return type and can thereby be used as a value and be passed as an argument to other functions.
In Swift, functions are defined using the func
keyword followed by the name of the function, arguments separated by commas, and a return type.
The arguments are defined by an argument label, a parameter name followed by a colon and then a type.
The return type is defined by a ->
followed by the type of the return value.
func functionName(argumentName parameterName: ArgumentType) -> ReturnType {
// function body
}
Swift arguments are a bit special compared to other languages, they use argument labels.
The argument label is used when calling the function.
The parameter name is used inside the function body to refer to the argument value.
If you only assign one name to the argument it will be used as both the argument label and the parameter name.
When assigning the argument label name as: _
the argument will not have a label when calling the function, e.g: functionName(argumentValue)
To call a function you use the function name followed by the argument label and the argument value, like this:
func functionName(argumentLabel parameterName: ArgumentType) -> ReturnType {
// function body
}
// calling the function
functionName(argumentLabel: argumentValue)
All arguments are immutable by default and can't be changed inside the function.
When a function has a return type be sure to return a value of that type.
That is done by using the return
keyword followed by the value to return.
func addTen(number value: Int) -> Int {
return value + 10
}
// calling the function
addTen(number: 10) // Returns 20
When functions don't return a value they have a return type of Void
, which is the same as ()
, but the return type can also be omitted in these cases.
Swift supports two types of comments.
Single line comments are preceded by //
and multiline comments are inserted between /*
and */
.