Swift Collection Types

Swift – Collection Types.

Working with Swift Collection Types is much elegant and simpler as compared to Objective C.
Following Code snippets works with Swift- Array Collection Type

Creating and Working with Array in Swift

    func testArrayCollectionInSwift()
    {
        //String Array
        //In the below code we are specifically stating the array contains string objects
        //Creating a array with values
        var stringArray:String[] = ["John Doe","Jane Doe","Mary Jane","Debasis Das"]
        println(stringArray)
        //Prints
        //[John Doe, Jane Doe, Mary Jane, Debasis Das]
        
        //In the below code type inference is used as type of array is not stated explicitly
        var nameArray = ["John Doe","Jane Doe","Mary Jane","Debasis Das"]
        println(nameArray)
        //Prints [John Doe, Jane Doe, Mary Jane, Debasis Das]
        
        //Modifying the content of an array
        nameArray.append("Nishant Singh")
        println(nameArray)
        //Prints [John Doe, Jane Doe, Mary Jane, Debasis Das, Nishant Singh]
        
        //modifying a array using + assignment operator
        var monthArray = ["Jan","Feb"]
        monthArray += "Mar"
        println(monthArray)
        //Prints [Jan, Feb, Mar]
        
        //Adding Array to an Array of Compatible Type
        monthArray += ["Apr","May"]
        println(monthArray)
        //Prints [Jan, Feb, Mar, Apr, May]
        
        //Accessing an element of an Array
        var firstMonth = monthArray[0]
        println(firstMonth)
        //Prints Jan
        
        //Setting the value of an array using index
        monthArray[0] = "Dec"
        println(monthArray)
        //Prints [Dec, Feb, Mar, Apr, May]
        
        //Inserting an object to an array at a stated index
        monthArray.insert("Mid Mar", atIndex: 4)
        println(monthArray)
        //Prints [Dec, Feb, Mar, Apr, Mid Mar, May]
        
        //Removing Objects from an Array
        monthArray.removeAtIndex(0)
        monthArray.removeAtIndex(1)
        println(monthArray)
        //Prints [Feb, Apr, Mid Mar, May]
        //Any gaps in an array is closed when objects are removed.
        
        //removing the last object of an array
        monthArray.removeLast()
        println(monthArray)
        //[Feb, Apr, Mid Mar] 
        //removeLast should be used to avoid use of array count property
        
        //Iterating through an array approach 1
        //Enumerating an array does not require ()
        for month in monthArray
        {
            println(month)
        }
        /*Prints
        Feb
        Apr
        Mid Mar
        */
    
        //Iterating through an array approach 2
        for (index, value) in enumerate(monthArray) {
            println("Item \(index + 1): \(value)")
        }
        /* Prints
        Item 1: Feb
        Item 2: Apr
        Item 3: Mid Mar
        */
        
        //Creating a blank array and adding items to it in Swift
        var daysArray = String[]()
        daysArray.append("Monday")
        println(daysArray)
        //Prints [Monday]
        
        //Creating an Array of a particular size and setting a default value
        var salaryArrayOne = Int[] (count:2, repeatedValue:10)
        println(salaryArrayOne)
        //Prints [10, 10]

        var salaryArrayTwo = Int[] (count:3, repeatedValue:11)
        println(salaryArrayTwo)
        //Prints [11, 11, 11]
        
        var mergedSalaryArray = salaryArrayOne + salaryArrayTwo
        println(mergedSalaryArray)
        //Prints [10, 10, 11, 11, 11]
    }

Creating and Working with Dictionary in Swift

    func testDictionaryCollectionInSwift()
    {
        var employeeDictionary: Dictionary <String,String> = ["firstName":"John","lastName":"Doe"]
        println(employeeDictionary)
        //Prints [firstName: John, lastName: Doe]
        
        //Type Inference
        var nameDictionary = ["firstName":"John","lastName":"Doe"]
        println(nameDictionary)
        //Prints [firstName: John, lastName: Doe]
        
        var count = nameDictionary.count;
        println(count)
        //Prints 2

        //Changing the value of a dictionary
        nameDictionary["firstName"] = "Steve"
        println(nameDictionary)
        
    
        //Iterating Over a dictionary
        for (fName, lName) in nameDictionary
        {
            println("\(fName) - \(lName)")
        }
        /*
        firstName - Steve
        lastName - Doe
        */
        
        //Iterating over a dictionary and accessing only the keys
        for fname in nameDictionary.keys
        {
            println("key is \(fname)")
        }
        /*
        key is firstName
        key is lastName
        */

        //Iterating over a dictionary and accessing only the values
        for name in nameDictionary.values
        {
            println("name component is \(name)")
        }
        /*
        name component is Steve
        name component is Doe
        */

        
        //Creating a blank dictionary and adding key values
        var newDictionary = Dictionary <Int, String>()
        newDictionary[1] = "One"
        newDictionary[2] = "Two"
        println(newDictionary)
        //Prints [1: One, 2: Two]
    }

Swift – Creating an Array of Dictionaries

var dataArray = [["firstName":"Debasis","lastName":"Das","email":"debasis_das@knowstack.com"],["firstName":"John","lastName":"Doe","email":"jdoe@knowstack.com"],["firstName":"Jane","lastName":"Doe","email":"janedoe@knowstack.com"],["firstName":"Mary","lastName":"Jane","email":"mjane@knowstack.com"]]

Posted in Cocoa, iOS, 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