New programming language for iOS, OS X, and watchOS app development
Similar to C and Objective-C
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"
var x = 0, y = 0, z = 0
let a = 1, b = 1, c = 1
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"
var red, green, blue: Double (Double is type of a value which represents 64-bit floating-point Number
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
let cat = "meow"; print(cat)
//prints "meow"
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
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']
var shoppingList = ['eggs', 'milk', 'flour', 'juice', 'apple']
for item in shoppingList {
print(item)
}
//eggs
//milk
//flour
//juice
//apple
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
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"]
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
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
personName: String refers to type value of argument that is passed in as argument and represents return type is a String
As you can see, Swift is very similar to JavaScript and any JavaScript developers can easily learn Swift!
There are bootcamps and schools out there teaching Swift language
according to Crunch Base...
Hack Reactor has also acquired Mobile Makers Academy