Collections
Array Updates
Arrays keep ordered values and can be appended to or updated by index.
Change an ordered list
array_updates.swift
let firstScore =
var scores = [firstScore, 10, 14]
scores.append(16)
scores[1] = scores[1] + 2
let total = scores[0] + scores[1] + scores[2] + scores[3]
print("scores=\(scores)")
print("total=\(total)")
let firstScore =
var scores = [firstScore, 10, 14]
scores.append(16)
scores[1] = scores[1] + 2
let total = scores[0] + scores[1] + scores[2] + scores[3]
print("scores=\(scores)")
print("total=\(total)")
let firstScore =
var scores = [firstScore, 10, 14]
scores.append(16)
scores[1] = scores[1] + 2
let total = scores[0] + scores[1] + scores[2] + scores[3]
print("scores=\(scores)")
print("total=\(total)")
arrays
Use an array when order matters. `append` adds a new element, and subscript assignment changes an element at a known index.