Below is an example of using basic Array functionalities on Array of Structs.
Example
// DECLARE STRUCT
struct Person {
let id : Int
let name : String
let age : Int
}
// DECLARE ARRAY
var elements : [String] = [] //Declare empty Array
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 ELEMENTS
let element = people[2] //Read element at index 2
people[2] = Person( id:0, name:"NEW PERSON", age:20) //Replace element at index 2
// PRINT ARRAY
print(element)
print(people)
dump(people)
// 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)") }