Using Arrays

This tutorial relies on you having completed the Simple Class tutorial. All new code should be added to this project.

Currently the LiskovPhraseMaker class allows us to set the type and subtype, but each time we overwrite the previous types. Ideally we want to store the type and subtype together as a pair, there are a number of ways of doing this, such as using dictionaries or multidimentional arrays, but the cleanest is to create a class to represent a one of the 'pairs' and store that in a collection. This tutorial uses a Array as the type of collection, but we could also use a set. (A Set may be preferable in this case, but requires some additional work).

Creating a class to represent a type - subtype pair

  1. Create a new class in Swift (preferably in a new file), called TypePair
  2. Add to it two properties named Type and Subtype, set the type to string but do not give them a value. The class should appear as shown below, but Xcode will be displaying a warning indicating that TypePair has no initialisers. This is because the type and subtype variables are not declared as optionals, but if an instance of the class was created, no value would be set for them:
    class TypePair {
        
        var type :String
        var subtype : String
    }
  3. We need to add an initialiser so that when the class is created we can set values for type and subtype. In many other languages this is known as a constructor. The keyword for an initialiser in Swift is init. Add the following initialiser function to the TypePair class:
    init(typeName: String, subtypeName: String){
    type = typeName
    subtype = subtypeName
    }

Creating an Array, storing the and retrieving the type pairs

  1. Inside the LiskovPhraseMaker class create an array property to store the TypePairs as follows:
    private var types = Array<TypePair>()
  2. Add code to the setTypeName(typeName: subtypeName:) method to create a TypePair class and add it to the Array as follows:
    var typePair = TypePair(typeName: self.type, subtypeName: self.subtype)
    types.append(typePair)
  3. Finally add a method which will return an array of Strings with the pairs in the format subtype: type. An example of code to do that is shown below:
    func allPairs() -> [String]{
            
        var stringArray = [String]()
            
        for pair in types{
            var pairString = pair.subtype + ": " + pair.type
            stringArray.append(pairString)
        }
            
        return stringArray
    }
  4. You should now be able to go back to the Main.swift file and add code to set a few different types, then iterate through the allPairs array and log them to the console. You should be able to write this code yourself, but if you are struggling
    phraseMaker.setTypeName("Cat", subtypeName: "Feline")
    phraseMaker.setTypeName("Dog", subtypeName: "Canine")
     
    var pairArray = phraseMaker.allPairs()
     
    for pair in pairArray{
        println(pair)
    }

Further tasks

Advanced Task

Consider the following type and subtype pairs:

Develop the application to provide example subtypes which are further level of inheritance away, e.g. Type: Vehicle, Subtype: Hatchback

Further advanced task

Implement the fuctionality, but use a Set, rather than an Array to store the pairs - this has the advantage that it cannot store duplicates, but also requires some additional protocols to be implemented