Arrays: who knew they could be this easy?


arrayName = [item1 , item2, ...]
arrayName: Array<T> = [item1, item2, item_n]
arrayName: T[] = [item1, item2, itim_n]

Arrays are much easier to define and deal with in Swift, when compared to Objective-C. As with any Swift variable/constant, you simply declare "var" or "let"...give the Array a name, then fill the array with values.
The Type of Array can either be declared after the name or can be inferred by the values in the Array.
When declaring the Array Type, the Type can either be stated in standard format or in shorthand (see examples below).
Unlike Arrays in Objective-C, Swift Arrays are Type-safe, and can only contain values of the same type.
Arrays can also be intialized with no values (empty), or with default values (see examples below).

//Array with Type declared in standard format:
var greetings : Array<String> = ["Hi","Aloha","Ciao"]

//Array with Type declared with shorthand syntax:
var greetings : String[] = ["Hi","Aloha","Ciao"]

//Array with Type inferred:
var greetings = ["Hi","Aloha","Ciao"]

//Initializing an empty Array with no values
var greetings = [String]()

//Initializing an Array with default values
var greetings = [Int](count: 5, repeatedValue: 0)
//The greetings Array would be initialized as [0,0,0,0,0]

Modifying and Accessing Arrays: Arrays and their Values can be added/moified/removed/accessed using Properties, Methods, SubScript and Operators.


//using the count property to return the number of elements in an Array:
var greetings: Array<String> = ["Hi","Aloha","Ciao"]
println("There are \(greetings.count) values in the greetings Array")
//prints "There are 3 values in the greetings Array"

//using the isEmpty property to determine if an Array has any values
if greetings.isEmpty{
 println("The greetings Array is Empty")
} else {
 println("The greetings Array has values")

//using .first to return the first value in the Array
println("The first value in greetings is \(greetings.first)")

//using .last to return the last value in the Array
println("The last value in greetings is \(greetings.last)")


/* use the append() method to add value(s) to the end of an Array */
var greetings: Array<String> = ["Hi","Aloha","Ciao"]
//prints '["Hi","Aloha","Ciao","Hola"]'

/* use the insert(atIndex:) method to add a value to an Array, at a specific location in the Array */
greetings.insert("Bonjour" atIndex:2)
//prints '["Hi","Aloha","Bonjour","Ciao","Hola"]'

/* use removeAtIndex() to remove and return a value from the Array, at a specific index */
let removedVal = greetings.removeAtIndex(2)
println("\(removedVal) has been removed from the greetings Array. The new Array is now \(greetings)")
/* prints 'Bonjour has been removed from the greetings Array. The new Array is now ["Hi","Aloha","Ciao","Hola"]' */

//use the reverse() method to reverse the values in an Array
println("the greetings Array is now: \(greetings)")
//prints 'the greetings Array is now: ["Hola","Ciao","Aloha","Hi"]

//perform an array mapping
let salutations ={
  (var phrase) -> String in
  return "\(phrase) Amigo!"
/* salutations Array is: ["Hola Amigo!","Ciao Amigo!","Aloha Amigo!","Hi Amigo!" */

//using the sort() method to sort an Array
println("Here are the sorted greetings: \(greetings)")
/* prints "Here are the sorted greetings: [Aloha, Ciao, Hi, Hola] */

/*using the sorted() method to return a new sorted Array, from an existing Array */
var sortedArray = greetings.sorted({$0 < $1})
/* returns a new Array called 'sortedArray' with values [Aloha, Ciao, Hi, Hola] */


//using the += operator to add new value(s) to an Array
var greetings = ["Hi","Aloha","Ciao"]
greetings += "Bonjour"
//compiler error because "Bonjour" is not an Array

greetings += ["GDay","Top o the Mornin","Buenos Dias"]
//the Array is now ["Hi","Aloha","Ciao","GDay","Top o the Mornin","Buenos Dias"]


//accessing the second value in an Array
var greetings = ["Hi","Aloha","Ciao"]
//prints: Aloha

//setting an Array value at a specific index, using SubScript
greetings[2] = "Bonjour"
//the greetings Array is now ["Hi","Aloha","Bonjour","Ciao"]

//adding new values in a specific index range
greetings[2...3] = ["Buenos Dias","G'Day"]
//the Array is now ["Hi","Aloha","Buenos Dias","G'Day","Bonjour","Ciao"]

Iterating through Arrays

//using for-in loop to iterate through values in an Array
var greetings = ["Hi","Aloha","Ciao"]
for greeting in greetings{
 println("This greeting value is: \(greeting)")

//using enumerate to return (index,value) Tuple to iterate through
for (greetingIndex,greetingValue) in enumerate(greetings){
 println("The greeting at index \(greetingIndex) is \(greetingValue)")