Swift language: Array

Arrays is one of the two collection types provided by swift language. By collection types, we meant to say that both array and dictionary can hold collection of data. The major difference between array and dictionary in swift with array and dictionary in objective c is that in swift array and dictionary are type dependent. More clearly you have to declare the type of data that array and dictionary can hold whereas in objective c both array and dictionary did not provide any information about the nature of object they stored.

Arrays:

 Array can store value in an ordered list of same type. Same value can be appear in an array multiple times in different position. In Swift , the type of value that an array going to store is always made clear. For example if you want an array to store int value then you can only add int values and no other data type values are stored in the array. Swift array are always type safe and they always made clear about the data they are going to store.

Array Syntax:

You can create array in swift using two type of syntax declaration

Array here sometype is datatype of array. For example Array

sometype[] , this is known as shorthand form and is most commonly used.

The example below creates an array called books

var books:String[] = [“Math”,”Science”]

Above code creates an array named  books and store string values. This array can only store string values and no other value then string are allowed to store in the array.

If you are creating array with literals then there is no need to declare data type for array as Swift will automatically chose its type based on literals value.

var books = [“Math”, “Science”]

In the above example Swift automatically chose String as books array data type because we  are giving string literals.

Initialize syntax for an array :

 To create an empty array without setting any initial value, you can use initializer syntax

 var books = String[]()

In the above line books is an array of String type as it is set to output  String values as initializer is a String type.

Get count of an array :
 To get count of an array you can use  count property of the array

println(“count of books is/(books.count)”)

Check for an empty array :

To check whether an array is empty of not you can use isEmpty property, this will return you a Boolean value

if books.isEmpty
{
    println(“books is empty)
}
else
{
    println(“books contain some value)
}

Add data to an array :

You can add new time to an array by using its append function

books.append(“History”)

append method will add new item to array at last index.

you can also add item to an array using assignment operator(+=)

books += “drawing”

Swift provide a very convenient way to add multiple items to an array

book += [“English”,”General Knowledge”]

Retrieve data from an array :

To get data from array you can use below code


let bookName = books[0]
the above line of code will return  Math
Remove and insert data to  an array at a particular index:

To insert an item at a particular index in an array you can use insert(atIndex:) method and to remove an item from a particular index you can use removeAtIndex method.

book.insert(“New”, atIndex:4)

book.removeAtIndex(2)

To remove last object from an array you can use removeLast method

books.removeLast()