Arrays
An array is a collection of objects - and the ordering of objects in an array is important. The following statement shows an array containing 3 items:
var OSes = ["iOS", "Android", "Windows Phone"]
In Swift, you create an array using the [] syntax. The compiler automatically infers the type of items inside the array; in this case it is an array of String elements.
Note that if you attempt to insert an element of a different type, like this:
var OSes = ["iOS", "Android", "Windows Phone", 25]
The compiler will assume the elements inside the array to be of protocol type AnyObject (which is similar to id in Objective-C and System.Object in .NET), which is an untyped object.
In general, most of the time you want your array to contain items to be of the same type, and you can do so explicitly like this:
var OSes:String[] = ["iOS", "Android", "Windows Phone"]
This forces the compiler to check the types of elements inside the array and flag an error when it detects otherwise.
The following example shows an array of integers:
var numbers = [0,01,2,3,4,5,6,7,8,9]
To retrieve the items inside an array, specify its 0-based index, like this:
var item1 = OSes[0] // "iOS"
var item2 = OSes[1] // "Android"
var item3 = OSes[2] // "Windows Phone"
To insert an element at a particular index, use the insert() function:
OSes.insert("BlackBerry", atIndex: 2)
item3 = OSes[2] // "BlackBerry"
var item4 = OSes[3] // "Windows Phone"
Note that in the above function call for insert(), you specify the parameter name - atIndex. This is known as an external parameter name and is usually needed if the creator of this function dictates that it needs to be specified.
We shall talk more about this when we discuss functions.
To change the value of an existing item in the array, specify the index of the item and assign a new value to it:
OSes[3] = "WinPhone"
To append an item to an array, use the append() function:
OSes.append("Tizen")
Alternatively, you can also see the += operator to append to an array, like this:
OSes += "Tizen"
You can append an array to an existing array, like this:
OSes += ["Symbian", "Bada"]
var lengthofArray = OSes.count
var arrayIsEmpty = OSes.isEmpty
You can also remove elements from an array using the following functions:
OSes.removeAtIndex(3) // removes "WinPhone"
OSes.removeLast() // removes "Bada"
OSes.removeAll(keepCapacity: true) // removes all element
For the removeAll() function, it clears all elements in the array. If the keepCapacity parameter is set to true, then the array will maintain its size.
Mutabilities of Arrays
When creating an array, its mutability (its ability to change its size after it has been created) is dependent of you using either the let or var keyword. If you used the let keyword, the array is immutable (its size cannot be changed after it has been created) as you are creating a constant. If you used the var keyword, the array is mutable (its size can be changed after its creation) as you are now creating a variable.
OK for today! Look out for the next lesson soon!
No comments:
Post a Comment