Collection Types
Collection Types
Arrays
Creating an Array by Adding Two Arrays Together
We can create a new Array with two Arrays in same types by the operator (+
)
var threeDoubles = Array(repeating: 0.0, count: 3)
// threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles is of type [Double], and equals [2.5, 2.5, 2.5]
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
Accessing and Modifying an Array
We can use subcrip syntax to get or change a series values, amazing for this.
var shoppingList: [String] = ["Eggs", "Milk"]
// shoppingList has been initialized with two initial items
shoppingList[2...4] = ["Bananas", "Apples"]
// shoppingList now contains 4 items
Iterating Over an Array
If we want get the item and item index both, we can use enumerated()
method, which will return a tuple including the item and item index.
for (index, value) in shoppingList.enumerated() {
print("Item \(index + 1): \(value)")
}
Sets
no order, but an item only appears once, which is the difference from Array.
Hash Values for Set Types
The Sets only be able to store hashable types, which means, if we want to store some we own type, we need to make the type hashable, where we can use the Hashable Protocol
to implement.
As for the String, Int, Double, Bool or something else for Swift’s basic types, which are all hashable by default.
Creating and Initializing
Unlike arrays, sets don’t have an equivalent shorthand form.
var letters = Set<Character>()
print("letters is of type Set<Character> with \(letters.count) items.")
// Prints "letters is of type Set<Character> with 0 items."
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
// favoriteGenres has been initialized with three initial items
var favoriteGenres2: Set = ["Rock", "Classical", "Hip hop"]
//all values in the array literal are of the same type, Swift can infer that Set<String> is the correct type to use for the favoriteGenres variable.So no need to write the type
Iterating Over a Set
The Set type doesn’t have a defined ordering, but we can use the sorted
method to make it order, this method will return the set’s elements as an array sorted by ascending.
var genre: Set = ["Jazz", "Classical", "Hip hop"]
for genre in favoriteGenres.sorted() {
print("\(genre)")
}
// Classical
// Hip hop
// Jazz
Fundamental Set Operations
We can see the illustrations below, which show the method of fundamental set operations.
../_images/setVennDiagram_2x.pngSet Membership and Equality
let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "🐱"]
let cityAnimals: Set = ["🐦", "🐭"]
let cityAnimals2: Set = ["🐦", "🐭"]
houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true
let isEqual = (cityAnimals == cityAnimals2)
// true
Dictionaries
All is familiar enought for me, will not mention it here.
Let’s think!