Creating Custom Classes

I know that I promised to make my entry much sooner than this but, once again, life got in the way! So here we are, a couple of weeks later than planned. Anyway, it's the first time in months that I'm actually doing this blog alongside some coding. So it's a step in the right direction! Moving forward, after working through this book, I plan to do this book alongside another course or text, so hopefully the 'live' recording of it will be useful and purposeful. 

This chapter is all about creating custom classes. Now I know, more or less, what a class is. From what I recall, a class bundles together data e.g. variables, constants, functions...all to then create specific objects or instances. We'll see what this chapter brings...!

So the task in this chapter is to create a functioning calculator - able to add, subtract etc.

class Calculator {
    

}

This is how to set up the class. The syntax is have a capital letter - different to variables, functions etc. Upper Camel Case, rather than camelCase. 

One of the properties inside the curly brackets needs to be the total. The two different types of properties are:

  • Stored properties - most common; store a variable or a constant
  • Computed properties - used to manipulate and retrieve values from other properties e.g. fullName would concatenate two other 'name' properties together e.g. fullName = firstName + secondName etc. 

var total: Double = 0.0


This is what goes inside the bracket. It's made explicitly type Double and 0.0 is used. Is this necessary? Type inference would cover that surely?

Anyway, something else here is the whole local/global thing. This is a local property/variable, as it is only accessed within the class of Calculator (unless another class with Calculator as the superclass were created). So I couldn't use 'total' outside of the class basically. 

The next thing is to bring in a method. Some recap of using the 'func' keyword; using double brackets () to initialise it etc. Also, it mentioned the two types of methods - type and instance methods. That's quite vague now, but I know it has something to do with creating an instance.... Anyway, for the example of total, a function is needed to return a value. To return anything in a function the -> arrow is needed. That makes sense. The parameters are what are put into the input of the function, not what is returned. So it could be...

func addToTotal(value: Double) 


In this example, the 'value' will come up as a parameter, which will require a value of type Double to be inputted. You can put in multiple parameters, separated with commas. These need to be filled out each time the function is used! 

You can also put in labels for each of the parameters. These are called the argument labels for the parameter names. Without writing both, then the parameter name will be used as the argument label - same for both basically. Not using an argument label at all can be done by using _ instead - that means when calling the method, no labels come up at all!

So it mentions Type Methods again as something that is called directly onto the class (rather than with an instance of the class). That clarifies that concept a little more for me. A class method here would be to add the function directly to a class that has been created, then the () to initialise it.

class Calculator {
    
    var total: Double = 0.0
    
    func clear () {
        
        self.total = 0.0
    }
    

}

In the example above, self is used to reference the property. Here is the code below for doing various other functions within the code:

//Adds the value to the total
    func addToTotal(value: Double) -> Double {
        
        self.total += value
        
        return self.total
    }
    
    //Subtracts the value from the total
    
    func subtractFromTotal(value: Double) -> Double {
        
        self.total -= value
        
        return self.total
    }
    
    //Multiplies the value by the total
    
    func multiplyByTotal(value: Double) -> Double {
        
        self.total *= value
        
        return self.total
    }
    
    //Divides value into the total
    
    func divideIntoTotal(value: Double) -> Double {
        
        self.total /= value
        
        return self.total
    }
}

The next aspect the eBook goes on to describe is parameters: optional and variadic. Calling the method and passing an argument...not too sure on this at the moment. 

OK, the variadic parameters bit makes more sense - you can have e.g. Double... as the type that is passed in, and that means unlimited numbers that can be put into the function; as opposed to a fixed number. 

Another parameter is the 'inout' parameter. This is for changing the value of the parameter. Useful for methods that change values in multiple collections. 

Here is the challenge example. I needed some help as I had overcomplicated it...

class Thermometer {
    
    
    func fahrenheitToCelsius (fahrenheit: Double) -> Double {
        
        return (fahrenheit - 32) * (5 / 9)
    }
    
    
    func celsiusToFahrenheit (celsius: Double) -> Double {
        
        
        return (celsius * 9) / 5 + 32
        
    }
}

let temp = Thermometer()

temp.celsiusToFahrenheit(celsius: 44)


So in the example above, a class of Thermometer is created, then two functions of going from fahrenheit to celsius, then vice versa. A constant of 'temp' is created, which calls the Thermometer class, then calls the method of going from celsius to fahrenheit... It does make sense but I need some more practise of creating classes like this. 


Next time, I will look at 'Unit Testing Your Code'. This WILL happen again this week! Now that I'm back and have made time for coding, it's going to be more regular!

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)