var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D"]
var setC = setA.subtracting(setB)
print(setC)
// print: ["B", "A"]
Arrays and collections
.subtracting() Subtraction
.symmetricDifference() Symmetric difference
var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D", "E", "F"]
var setC = setA.symmetricDifference(setB)
print(setC)
// print: ["B", "E", "F", "A"]
.union()
var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D", "E", "F"]
var setC = setA.union(setB)
print(setC)
// print: ["B", "A", "D", "F", "C", "E"]
.intersection() Intersection
var setA: Set = ["A", "B", "C", "D"]
var setB: Set = ["C", "D", "E", "F"]
var setC = setA.intersection(setB)
print(setC) // print: ["D", "C"]
.isEmpty property
var emptySet = Set<String>()
print(emptySet.isEmpty) // print: true
var populatedSet: Set = [1, 2, 3]
print(populatedSet.isEmpty) // print: false
Iterate over a collection
var recipe: Set = ["Egg", "Flour", "Sugar"]
for ingredient in recipe {
print ("Include \(ingredient) in the recipe")
}
.contains()
var names: Set = ["Rosa", "Doug", "Waldo"]
print(names.contains("Lola")) // print: false
if names.contains("Waldo"){
print("There's Waldo!")
} else {
print("Where's Waldo?")
}
// print: There's Waldo!
.remove() and .removeAll() methods
var oddNumbers: Set = [1, 2, 3, 5]
// remove existing element
oddNumbers.remove(2)
// remove all elements
oddNumbers.removeAll()
.insert()
var cookieJar: Set = [
"Chocolate Chip",
"Oatmeal Raisin"
]
// add a new element
cookieJar.insert("Peanut Butter Chip")
Populate the collection
var vowels: Set = ["a", "e", "i", "o","u"]
To create a set filled with values, use the Set
keyword before the assignment operator.
Empty collection (Set)
var team = Set<String>()
print(team)
// print: []
Collection (Set)
var paintingsInMOMA: Set = [
"The Dream",
"The Starry Night",
"The False Mirror"
]
We can use a collection (Set
) to store unique
elements of the same data type
Traverse the array
var employees = ["Peter", "Denial", "Jame"]
for person in employees {
print(person)
}
// print: Peter
// print: Denial
// print: Jam
.insert() and .remove() methods
var moon = ["ð", "ð", "ð", "ð"]
moon.insert("ð", at: 0)
// ["ð", "ð", "ð", "ð", "ð"]
moon. remove(at: 4)
// ["ð", "ð", "ð", "ð"]
.append() method and += operator
var gymBadges = ["Boulder", "Cascade"]
gymBadges.append("Thunder")
gymBadges += ["Rainbow", "Soul"]
// ["Boulder", "Cascade", "Thunder",
// "Rainbow", "Soul"]
Initialize with array literal
// use type inference:
var snowfall = [2.4, 3.6, 3.4, 1.8, 0.0]
// explicit type:
var temp: [Int] = [33, 31, 30, 38, 44]
index
The index refers to the item's position in the ordered list, and a single element is retrieved from the array using the subscript syntax array[index]
.
var vowels = ["a", "e", "i", "o", "u"]
print(vowels[0]) // prints: a
print(vowels[1]) // prints: e
print(vowels[2]) // print: i
print(vowels[3]) // prints: o
print(vowels[4]) // prints: u
Note: Swift arrays are zero-indexed, meaning the first element has index 0.
.count property
var grocery = ["ð¥", "ð¥", "ðª", "ð¥", "ð"]
print(grocery.count)
// print: 5
Array array
var scores = [Int]()
// array is empty: []
Comments