Swift
New programming language for iOS, OS X, and watchOS app development
Similar to C and Objective-C
Why use Swift over Objective-C?
- easier to pick up
- easier to maintain
- code is more understandable for beginner programmers
- looks nice
- will replace C for embedded programming on Apple platforms
- has elements of functional programming language (i.e map and filter functions)
Lets Dive in!
Basics
Constants and Variables
constants cannot be changed once it is set, whereas a variable can be set to a different value in the future
constant
let foo = 10
variable
var baz = "Hello World"
you can declare multiple constants or multiple variables on a single line separated by commas
var x = 0, y = 0, z = 0
let a = 1, b = 1, c = 1
type annotations
you can provide type annotation when you declare a constant or variable, to make it clear what type of values the constant or variable will be
var thisIsAString: String
after type annotation is set, you can now declare a value to the variable
thisIsAString = "Hello World"
Define multiple related variables of same type on single type
var red, green, blue: Double (Double is type of a value which represents 64-bit floating-point Number
- constants and variable can contain almost any character except that it cannot contain white space characters, mathematical symbols, arrows, invalid unicode code points, or line and box drawing characters

comments in swift
you can either use double forward slash for single line commenting or forward slash followed by asterisk and end with asterisk follow by a forward slash for multi-line comments


semicolons; is not required after each statement, only required if you want to write multiple separate statements on single line
let cat = "meow"; print(cat)
//prints "meow"
collection types
-
arrays
-
sets
-
dictionaries
if you create an array, a set, or a dictionary and assign it to variable, the collection that is created is mutable, but if you assign it to constant, it is immutable and size and contents cannot be changed
Array
var myArray = [String]()
the String inside the brackets specify what type of value will be stored in the array.
To add a value to the array,
myArray.append("Hello World")
or
let helloWorld = "Hello World"
myArray.append(helloWorld)
to add value to end of array, you use append(), if you want to add it to front of the array, you can use insert(). You can also add it by index - you can use
var sampleArray = ['cat', 'dog', 'beaaaaaar']
sampleArray.insert('grizzly bearrrr', atIndex: 2)
sampleArray => ['cat', 'dog', 'grizzly bearrrr', 'beaaaaaar']
you can also remove a value from index and returns the removed item (can be ignored if you do not need return value)
sampleArray.removeAtIndex(1)
sampleArray => ['cat', 'grizzly bearrrr', 'beaaaaaar']
iterating over array
var shoppingList = ['eggs', 'milk', 'flour', 'juice', 'apple']
for item in shoppingList {
print(item)
}
//eggs
//milk
//flour
//juice
//apple
Sets
stores distinct values of same type in collection with no defined ordering. Sets should be used if order of items is not important and when you need to ensure an item only appears once
creating an empty set
Set<Element>, where element is the type that the set is allowed to store


Creating a set with an Array literal
var favoriteAnimals: Set<string> = ["bear", "dog", "cat"]
because of Swift's type inference, you don't always have to write the type of the set if you're initializing it with an array literal containing values of same type.
So we can write it like this:
var favoriteAnimals: Set = ["bears", "dogs", "cats"]
Iterating over Set
for animal in favoriteAnimals {
print(animal)
}
//prints
//bears
//dogs
//cats
Swift's Set type does not have a defined ordering, so if you want to iterate over values with specific order, you can use sort() method with your for in loop
for animal in favoriteAnimals.sort() {
print(animal)
}
//prints
//bears
//cats
//dogs
Dictionaries
dictionary stores associations between keys of same type and values of same type in a collection with no defined ordering. Each value is associated with unique key acting as an identifier for that value in dictionary. You should use dictionary when you need to look up values based on their identifier
Swift's dictionary is written as
Dictionary<Key, Value>
where key is type of value that can be used as dictionary key and value is type of value that dictionary stores for those keys
or simply [ Key: Value ]
Creating an Empty Dictionary

now you can add key value pairs by doing following

Creating Dictionary with Dictionary Literal
[key1: value1, key2: value2, key3: value3]
example:
var numbers: [Int: String] = [1: "one", 2: "two, 3: "three"]

Modifying Dictionary
to add it to current Dictionary airports
airports["LHR"] = "London"
//airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin", "LHR": "London"]
to change existing key value
airports["LHR"] = "London Heathrow"
//airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin", "LHR": "London Heathrow"]
to remove key value pair in dictionary
aiports.removeValueForKey("DUB")
//airports = ["YYZ": "Toronto Pearson", "LHR": "London Heathrow"]
Iterating over a Dictionary

you can also retrieve an iterable collection of dictionary's keys or values by .keys or .values properties

Functions

personName: String refers to type value of argument that is passed in as argument and represents return type is a String

Functions can also return multiple values

As you can see, Swift is very similar to JavaScript and any JavaScript developers can easily learn Swift!
Doesn't this all look very similar?
There are bootcamps and schools out there teaching Swift language
- Mobile Makers
- Treehouse
- Udacity
- One Month
- and many more
Interesting Fact
according to Crunch Base...

Hack Reactor has also acquired Mobile Makers Academy
deck
By Jisoo Yoon
deck
- 1,026