Swift Collection Type Array

Swift Collection Type Array

  • Collection Types in Swift
  • Swift has 3 primary collection types , known as arrays, sets and dictionaries
    • arrays – ordered collections of values
    • sets – unordered collection of values
    • dictionaries – unordered collections of key-value pairs

In this post we will see how Swift Array works

Swift Array

    func arrayTest1(){
        var intArray = [Int]()    //This line creates an empty array
        print(intArray)           //[]
        print(intArray)           //[10]
        print(intArray)           //[10, 20]
        intArray = []
        print(intArray)           //[] - At this moment the intArray is again empty

Swift – Creating a float array with the same repeated value

    func arrayTest2(){
        let floatArray = [Float] (count: 3, repeatedValue: 12.32)
        print(floatArray) //[12.32, 12.32, 12.32]

Swift Adding Two Arrays to create a third array

    func arrayTest3(){
        let array1 = [10,20]
        let array2 = [30,40]

        //Adding two arrays to create a new array
        let array3 = array1 + array2
        print(array3)     //[10, 20, 30, 40]

Creating an array using an Array literal

        func arrayTest4(){
        let nameArray:[String] = ["John  Doe","Jane Doe","Mary Jane"]
        let ageArray:[Int] = [22,23,21]
        print(nameArray)  //[John  Doe, Jane Doe, Mary Jane]
        print(ageArray)   //[22, 23, 21]
        //If we are initializing an array with the array literal then we do not have to write the type so for the above example can be re-written as 
        let nameArray1 = ["John  Doe","Jane Doe","Mary Jane"] //Note we have not used the array type
        print(nameArray1)  //[John  Doe, Jane Doe, Mary Jane]
        //In the above since all the objects are of the same type, Swift can infer that the type of the array is [String]

Accessing and Modifying an Array

    func arrayTest5(){
        var monthArray = ["Jan","Feb"]
        print("monthArray Count is \(monthArray.count)") //monthArray Count is 2
        if (monthArray.isEmpty){
            print("Month Array is empty")
            print("Month Array is not empty")
        //Month Array is not empty
        print(monthArray) //[Jan, Feb, Mar]
        monthArray += ["Apr","May"]
        print(monthArray) //[Jan, Feb, Mar, Apr, May]
        let firstMonth = monthArray[0] //First item of an array is present at index as Swift Arrays are zero indexed
        print("first month is \(firstMonth)") //first month is Jan
        //Change the value of the first object of the array
        monthArray[0] = "January"
        print(monthArray) //[January, Feb, Mar, Apr, May]
        print("first month is \(monthArray[0])") //first month is January
        //Please note in the example given below the replacement set has a different length than the range we are trying to replace
        monthArray[1...4] = ["February","March","April","May","June","July"]
        print(monthArray) //[January, February, March, April, May, June, July]
        //Removing an item at an index
        print(monthArray) //[February, March, April, May, June, July]

        //Inserting an item at an index
        monthArray.insert("January", atIndex: 0)
        print(monthArray) //[January, February, March, April, May, June, July]
        //Removing the last object of an array
        print(monthArray) //[January, February, March, April, May, June]   //Note July got removed
        //Iterating over an array
        for month in monthArray{
        //Getting the index of each element of an array while iterating over it
        for (index, value) in monthArray.enumerate(){
            print("index is \(index) and month is \(value)")
            index is 0 and month is January
            index is 1 and month is February
            index is 2 and month is March
            index is 3 and month is April
            index is 4 and month is May
            index is 5 and month is June
Posted in Swift Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts

Hit Counter provided by technology news