Array
Declaring
// Explicitly
var myArrayOfNames: [String]
// Or Explicitly initialized
var myArrayOfNames: [String] = [String]()
// Or Explicitly initialized with values
var myArrayOfNames: [String] = ["Mark", "Bob", "Matt"]
// Implicitly
var myArrayOfNames = [String]()
// Or Implicitly initialized with values
var myArrayOfNames = ["Mark", "Bob", "Matt"]
// Or with values (3 values with "No name yet")
var myArrayOfNames = [String] (count: 3, repeatedValue: "No name yet")
Modifying
// Add 3 names
myArrayOfNames = ["Mark", "Bob", "Matt"]
// Add one more to the end of the array
myArrayOfNames.append("Jeff")
// Add two more to the end of the array
myArrayOfNames += ["Julie", "Patty"]
// Add a name so it's the 2nd in the array
myArrayOfNames.insert("Patrick", atIndex: 1)
// Change the 4th name
myArrayOfNames[3] = "Courtney"
// Remove the 5th name
myArrayOfNames.removeAtIndex(4)
print("I have \(myArrayOfNames.count) items in my array")
print("The 2nd name is \(myArrayOfNames[1]).")
Iterating
for name in myArrayOfNames {
print(name)
}
// Just a range of indexes
for name in myArrayOfNames[0...2] {
print(name)
}
// Get the index position and the value
for (index, name) in myArrayOfNames.enumerate() {
print("\(name) is at position \(index)")
}
Dictionary
Declaring
// Say I want to hold Name and Age of a person.
// Explicitly
var peopleDictionary: [String:Int]
// Explicitly initialize an empty dictionary
var peopleDictionary: [String:Int] = [String:Int]()
// Implicitly
var peopleDictionary = [String:Int]()
// Implicitly with values
var peopleDictionary = ["Mark":44, "Bob":51, "Matt":45]
Modifying
// Add 3 people
peopleDictionary = ["Mark":44, "Bob":51, "Matt":45]
// Add one more
peopleDictionary["Jeff"] = 44
// Change Courtney's age
peopleDictionary["Courtney"] = 30
// Change Courtney's age and get the previous age
if let oldValue = peopleDictionary.updateValue(32, forKey: "Courtney") {
print("The old value for Courtney was \(oldValue).")
}
// Remove Jeff
peopleDictionary["Jeff"] = nil
print("I have \(peopleDictionary.count) items in my dictionary")
print("The age of Mark is \(peopleDictionary["Mark"]!).")
Set
Note: Not going to cover too much because I will probably use this less than arrays and dictionaries.
How they are different:
- Stores unique values (can not store duplicate values)
- Not ordered (You insert an item, no guarantee where it will end up in the collection.)
Declaring
// Explicitly
var mySetOfNames: Set<String>
// Or Explicitly initialized
var mySetOfNames: Set<String> = Set<String>()
// Or Explicitly initialized with values
var mySetOfNames: Set<String> = ["Mark", "Bob", "Matt"]
// Implicitly
var mySetOfNames = Set<String>()
// Or Implicitly initialized with values (Still need to say it is a Set)
var mySetOfNames: Set = ["Mark", "Bob", "Matt"]
Modifying
// Add a name
mySetOfNames.insert("Patrick")
// Remove a name
mySetOfNames.remove("Mark")
print("I have \(mySetOfNames.count) items in my set")
for name in mySetOfNames {
print(name)
}
(Updated for Swift 2.2)