Variables
var i : int = 0 //An integer type!
var c : char = ‘a’ // A character type!
var implicitInt = 70 // Swift can automatically create a variable type
var implicitDouble = 70.0 //This variable will be a double
Constants
let i : int = 0 //An constant integer type!
let c : char = ‘a’ // A constant character type!
let implicitInt = 70 // Swift can automatically create a constants too
let implicitDouble = 70.0 //This variable will be a double
Variables - printing values
var msg = “Hello, Swift”
print(“This is a debug message.”)
print(msg)
It is very useful when doing some debug. It is just show in the Xcode console while never show in actual application itself.
Common Operators
- Assignment operator (=)
- Arithmetic operators (+,_,*,/,%)
- Compound Assignment Operators (+=, -=, *=, /=)
- Boolean operators (==, !=, <=, >=, !, &&, ||)
Note: Swift DOES NOT Support Increment& Decrement Operators, ++ & — are not available as of Swift 3.0
Data Types - Integers
- Declared using keyword Int
Integer is default size on the platform being used
E.g. 32-bit OS = Int32, 64-bit OS = Int64
You can declare Integers with the following size:
—Int8, Int16, Int32, Int64
You can also declare unsigned Integers:
-UInt, UInt8, UInt16, UInt32, UInt64
Data Types - Floating Point Numbers & Boolean
Swift has two floating point types: Float and Double
— Floats have 32-bit floating point number precision
— Double have 64-bit floating point number precision
Booleans in Swift use the Bool keyword
— Value can be either true or false
Data Types - Strings
var age = 10
var name = “Alice”
let message = “Hello \(name)! You are \(age) years old!”
* Swift strings can also use the + operator to concatenate other values
* Strings are now returning to functioning like a collection. This means we now have access to functions like: count, isEmpty(), reversed(), dropFirst() etc.
Data Types - Tuples
Swift also supports tuples which combine multiple values into a single value. Example:
var statusCode = (404, “Not Found”)
let (code, description) = statusCode
print(code ) //Prints 404
print(statusCode.0) //Prints 404
print(description ) //Prints Not Found
print(statusCode.1) //Prints Not Found
Data Types - Optionals
var i : int? // An optional variable set to nil
* In Swift, variables cannot be nil(null)
* You must always declare the value of a variable in order for it to be created and compile.
* However, we may not want to do this (or we may want a variable to have a nil value)
* We can do this by using Optional variables
* Optional variables are now seen as being Optional(data type)
* To get the data out of an optional, we need to “Unwrap it”
Data Types - Optional Unwrapping
If we know a variable contains a value, we can “force unwrap” the variable to get the data out of it.
if number != nil
{
print(“The number is \(number!)”)
}
We can also perform optional binding to easily determine if value is set:
if let unwrappedNumber = int(number)
{
print(“The number is \(unwrappedNumber)”)
}
else
{
print(“Number cannot be unwrapped”)
}
Data Types - Arrays
Arrays in Swift can be declared in the following ways:
var array = Array<Int>()
var data = [ “Josh”, “Charlie”, “Jason”]
We can easily add and remove elements from an array by using the append and remove functions:
array.append(10)
array.removeLast()
Data Types -Dictionary
Dictionaries are a form of collection used by swift.
They have a structure of Name