Arrays

“var numbers: [Int8] = [1, -3, 50, 72, -95, 115] ”

.contains

“when you assign a collection to a constant using let: You can't add, remove, or modify any items within the collection. However, if you store the collection within a variable using var, you'll be able to add to, remove from, or modify items in the collection. In addition, you'll be able to empty the collection or set the variable to a different collection entirely. ”

Excerpt From: Apple Education. “App Development with Swift.” Apple Inc. - Education, 2017. iBooks. https://itunes.apple.com/au/book/app-development-with-swift/id1219117996?mt=11

“This example defines an array with traditional type annotation.

var myArray: [Int] = []  

This example defines an array using a special collection type annotation. This is a less common practice you should be familiar with in case you run across it in code you're working with.

var myArray: Array&<Int> = []

Just like all objects can be initialized by adding a () after the type name, you can add a () after [Int] to initialize an empty array of Int objects. You should also be familiar with this code in case you run across it in the future.

var myArray = [Int]()
var myArray = [Int](repeating: 0, count: 100)

.isEmpty

.append

.insert

names.insert("Bob", at: 0)

.removeAt

.removeLast

.removeAll

Nested Arrays

let array2 = [4,5,6]
let containerArray = [array1, array2] //[ [1,2,3], [4,5,6] ]
let firstArray = containerArray[0]  //[1,2,3]
let firstElement = containerArray[0][0] //1

Dictionaries

“var scores = ["Richard": 500, "Luke": 400, "Cheryl": 800]   As with the two other collection types, a dictionary has a count property to determine the number of key/value pairs, and the isEmpty property returns true if there are no key/value pairs in the dictionary and false otherwise. The syntax for creating an empty dictionary will probably seem familiar by now:   var myDictionary = [String: Int]() ”

“var myDictionary = Dictionary<String, Int>()   var myDictionary: [String: Int] = [:]”

scores["Oli"] = 399

does a create or update,

“let oldValue = scores.updateValue(100, forKey: "Richard") ”

updateValue returns the old value, or nil if there wasn't one.

if-let

“Swift uses if-let syntax to let you run code only if a value is returned from the method. If there wasn't an existing value, the code within the brackets won't be executed:   if let oldValue = scores.updateValue(100, forKey: "Richard") { print("Richard's old value was \(oldValue)") }   ”

remove

“To remove an item from a dictionary, you can use subscript syntax, setting the value to nil. Similar to updating a value, dictionaries have a removeValue(forKey:) method if you need the old value returned before removing it:   var scores = ["Richard": 100, "Luke": 400, "Cheryl": 800] scores["Richard"] = nil ["Luke": 400, "Cheryl": 800]   if let oldValue = scores.removeValue(forKey: "Luke") { print("Luke's score was \(oldValue) before he stopped playing") } ” Excerpt From: Apple Education. “App Development with Swift.” Apple Inc. - Education, 2017. iBooks. https://itunes.apple.com/au/book/app-development-with-swift/id1219117996?mt=11 !!Keys and Values You can dot into collections of keys and values, assigning them to an array if you want to use them again. “var scores = ["Richard": 500, "Luke": 400, "Cheryl": 800]   let players = Array(scores.keys) ["Richard", "Luke", "Cheryl"] let points = Array(scores.values) [500, 400, 800] ” Excerpts From: Apple Education. “App Development with Swift.” Apple Inc. - Education, 2017. iBooks. https://itunes.apple.com/au/book/app-development-with-swift/id1219117996?mt=11 Swift Loops