0% found this document useful (0 votes)
6 views21 pages

2 05 Collections

Uploaded by

Abhijeet Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

2 05 Collections

Uploaded by

Abhijeet Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit 2—Lesson 5:

Collections
Collection types

Array
Dictionary
Arrays
Defining
[value1, value2, value3]

var names: [String] = ["Anne", "Gary", "Keith"]


Arrays
Defining
[value1, value2, value3]

var names = ["Anne", "Gary", "Keith"]


Arrays
Defining
var numbers = [1, -3, 50, 72, -95, 115]
Arrays
Defining
var numbers: [Int8] = [1, -3, 50, 72, -95, 115]
Arrays
contains
let numbers = [4, 5, 6]
if numbers.contains(5) {
print("There is a 5")
}

There is a 5
Array types

var myArray: [Int] = []

var myArray: Array<Int> = []

var myArray = [Int]()


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

let count = myArray.count

if myArray.isEmpty { }
Working with arrays
Accessing or setting a specific item
var names = ["Anne", "Gary", "Keith"]
let firstName = names[0]
print(firstName)

Anne

names[1] = "Paul"
print(names)

["Anne", "Paul", "Keith"]


Working with arrays
Appending
var names = ["Amy"]
names.append("Joe")
names += ["Keith", "Jane"]
print(names)

["Amy", "Joe", "Keith", "Jane"]


Working with arrays
Inserting
var names = ["Amy", "Brad", "Chelsea", "Dan"]
names.insert("Bob", at: 0)
print(names)

["Bob", "Amy", "Brad", "Chelsea", "Dan"]


Working with arrays
Removing
var names = ["Amy", "Brad", "Chelsea", "Dan"]
let chelsea = names.remove(at:2)
let dan = names.removeLast()
print(names)

["Amy", "Brad"]

names.removeAll()
print(names)

[]
Working with arrays

var myNewArray = firstArray + secondArray


Working with arrays
Arrays within arrays
let array1 = [1,2,3]
let array2 = [4,5,6]
let containerArray = [array1, array2]
let firstArray = containerArray[0]
let firstElement = containerArray[0][0]
print(containerArray)
print(firstArray)
print(firstElement)

[[1, 2, 3], [4, 5, 6]]


[1, 2, 3]
1
Add/remove/modify a dictionary
Adding or modifying
var scores = ["Richard": 500, "Luke": 400, "Cheryl": 800]

scores["Oli"] = 399

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


Add/remove/modify a dictionary
Adding or modifying
var scores = ["Richard": 500, "Luke": 400, "Cheryl": 800]

scores["Oli"] = 399

if let oldValue = scores.updateValue(100, forKey: "Richard") {


print("Richard's old value was \(oldValue)")
}

Richard's old value was 500


Add/remove/modify a dictionary
Removing
var scores = ["Richard": 100, "Luke": 400, "Cheryl": 800]
scores["Richard"] = nil
print(scores)

if let removedValue = scores.removeValue(forKey: "Luke") {


print("Luke's score was \(removedValue) before he stopped playing")
}
print(scores)

["Cheryl": 800, "Luke": 400]


Luke's score was 400 before he stopped playing
["Cheryl": 800]
Accessing a dictionary

var scores = ["Richard": 500, "Luke": 400, "Cheryl": 800]

let players = Array(scores.keys) //[“Richard", "Luke", "Cheryl"]


let points = Array(scores.values) //[500, 400, 800]

if let lukesScore = scores["Luke"] {


print(lukesScore)
}

400

if let henrysScore = scores["Henry"] {


print(henrysScore)
}
Unit 2—Lesson 5
Lab: Collections
Open and complete the exercises in Lab - Collections.playground
© 2021 Apple Inc.
This work is licensed by Apple Inc. under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

You might also like