2
2
.
.
2
2
.
.
3
3
C
C
o
o
l
l
l
l
e
e
c
c
t
t
i
i
o
o
n
n
s
s
I
I
n
n
f
f
o
o
Collection Data Types are implemented as Structures and allow us to group multiple items of the same Data Type.
Array Data Type holds ordered elements of same Data Type where each Element has unique integer Key
Set Data Type holds unordered elements of same Data Type where each Element is unique
Dictionary Data Type holds unordered elements of same Data Type where each Element has unique Key
A
A
r
r
r
r
a
a
y
y
Array Data Type is an ordered container that holds elements of the same data type.
Each Element has unique integer Key starting from 0. It can be created with Array Literal.
Array of Strings [R] [R]
// DECLARE ARRAY
var people : Array<String> = [] //Declare empty Array of Strings.
var people : [String] = ["ZERO", "FIRST"] //Declare Array of Strings. Initilize it with elements.
var people = ["ZERO", "FIRST"] //Declare Array. Data Type is implied by Array Literal.
// ADD/REMOVE ELEMENTS
people.append("SECOND") //Append element at the end of Array
people.append("THIRD") //Append element at the end of Array
people.insert("NEW" , at:2) //Insert element at index 2. Others are shifted.
people.insert("SUB ZERO", at:0) //Insert element at the begining of Array
people.remove(at:2) //Remove element at index 2. Others are shifted.
// GET/REPLACE ELEMENT
let element = people[2] //Read element at index 2
people[2] = "NEW PERSON" //Replace element at index 2
// ANALYZE
if(people.isEmpty) { print("Array is empty." ) }
var numberOfElements = people.count //5
// ITERATE THROUGH ARRAY
for person in people { print(person) }
for person in people.reversed() { print(person) }
for (index, person) in people.enumerated() { print("\(index) - \(person)") }
// PRINT ARRAY
print(element)
print(people) //["SUB ZERO", "ZERO", "FIRST", "NEW", "THIRD"]
dump (people) //Print Array
Array of structs
// DECLARE STRUCT
struct Person {
let id : Int
let name : String
let age : Int
}
// DECLARE ARRAY
var people : Array<Person> = [] //Declare empty Array of Person Instances.
var people : [Person] = [ //Declare Array.
Person( id:0, name:"ZERO" , age:0 ), //Initilize it with elements.
Person( id:1, name:"FIRST", age:10 )
]
// ADD/REMOVE ELEMENTS
people.append(Person( id:0, name:"SECOND" , age:20)) //Append element at the end of Array
people.append(Person( id:0, name:"THIRD" , age:30)) //Append
people.insert(Person( id:0, name:"NEW" , age:20), at:2) //Insert element at index 2. Others are shifted.
people.insert(Person( id:0, name:"SUB ZERO", age:20), at:0) //Insert element at the begining of Array
people.remove(at:2) //Remove element at index 2. Others are shifted.
// GET/REPLACE ELEMENT
let element = people[2] //Read element at index 2
people[2] = Person( id:0, name:"NEW PERSO", age:20) //Replace element at index 2
// ITERATE THROGUH ARRAY
for person in people { print(person) }
for person in people.reversed() { print(person) }
for (index, person) in people.enumerated() { print("\(index) - \(person)") }
// PRINT ARRAY
print(element)
print(people)
dump(people)
S
S
e
e
t
t
Set Data Type is unordered container that holds unique elements of the same Data Type.
It can be created with Array Literal.
Set [R]
// DECLARE SET
var people0 : Set<String> = [] //Declare empty String Set
var people : Set = ["John", "Lucy"] //Implicit element data type. Create set with array literal.
// ADD/REMOVE ELEMENT
people.insert("NEW PERSON") //Add element.
var element = people.remove("NEW PERSON") //Remove element. Optional("NEW PERSON")
// ANALYZE
if people.contains("John") { print("Element found.") }
if people.isEmpty { print("Set is empty." ) }
var numberOfElements = people.count //2
// ITERATE THROUGH SET
for person in people { print(person) } //John
// PRINT SET
print(element) //Optional("NEW PERSON")
print(people) //["John", "Lucy"]
D
D
i
i
c
c
t
t
i
i
o
o
n
n
a
a
r
r
y
y
Dictionary Data Type is unordered container that holds elements of the same Data Type.
Each Element has unique Key of the same data type (which can be different from Element's data type)
Dictionary [R]
// DECLARE DICTIONARY
var people0 : Dictionary<Int, String> = [:]
var people1 : [Int: String] = [12: "John", 23: "Lucy"]
var people = [12: "John", 23: "Lucy"]
// ADD/REMOVE ELEMENTS
people[120] = "NEW PERSON" //Add new element (if such key didn't exist).
var element = people.removeValue(forKey: 12) //Remove element with key 12. Returns Optional.
// READ/REPLACE ELEMENT
element = people[120] //Read element with key 12. Returns Optional.
people[12] = "REPLACED PERSON" //Replace element with key 12
// ANALYZE
if(people.isEmpty) { print("Dictionary is empty." ) }
var numberOfElements = people.count //2
// ITERATE THROUGH ARRAY
for person in people { print(person) } //(key: 23, value: "Lucy")
for (key, person) in people { print("\(key) : \(person)") } //23 : Lucy
for (key, person) in people.reversed() { print("\(key) : \(person)") } //120 : NEW PERSON
// PRINT ARRAY
print(element) //Optional("NEW PERSON")
print(people) //[12: "NEW PERSON", 23: "Lucy", 120: "NEW PERSON"]
dump (people)