Creating Objects from Classes
Well, it's been a little while! The last time I blogged about using Swift, I was on my Christmas Break from work. Predictably, the first few weeks of term have been busy so there has been little to no time to open up my mac and do anything coding related. So today is the first time for over a month. Too long!
I remember this feeling when trying to write regularly into a diary or journal years ago. There would always be the associated guilt of not updating it often enough. It would've been tempting today to have abandoned the idea of doing anything Swift based and go for the cliched 'I'll do it another time'. But as I've mentioned in previous blogs, I'm determined not to undo and unlearn all the knowledge that I had steadily built up in my sojourn from work recently.
Anyway, onwards and upwards. The next chapter I'm tackling is all about 'Creating Objects from Classes' (Chapter 6 - pages 83 to 94). As before, I'll do a 'live' blog of my thoughts along the way. Hopefully it will make some sort of sense!
The first part of the chapter explains how to create an instance of a class. Using Xcode (the demo file from before) with a specific line of code added in does not mean an awful lot to me. Anyway, I'll see where this goes....
Inside the brackets, pressing the 'esc' key gives two options. This means that I can select two options of 'arguments'. Another aspect is now typing in the new constant (textField) followed by a period (.) then allows properties of it to be accessed. OK, after a couple more lines of code, this is what we have:
I remember this feeling when trying to write regularly into a diary or journal years ago. There would always be the associated guilt of not updating it often enough. It would've been tempting today to have abandoned the idea of doing anything Swift based and go for the cliched 'I'll do it another time'. But as I've mentioned in previous blogs, I'm determined not to undo and unlearn all the knowledge that I had steadily built up in my sojourn from work recently.
Anyway, onwards and upwards. The next chapter I'm tackling is all about 'Creating Objects from Classes' (Chapter 6 - pages 83 to 94). As before, I'll do a 'live' blog of my thoughts along the way. Hopefully it will make some sort of sense!
The first part of the chapter explains how to create an instance of a class. Using Xcode (the demo file from before) with a specific line of code added in does not mean an awful lot to me. Anyway, I'll see where this goes....
let textField = UITextField()
let textField = UITextField()
textField.placeholder = "First Name"
textField.frame = CGRect(x: 20, y: 70, width: 280, height: 31)
So it means I have created a text box which is called 'First Name', it is rectangular in shape and has certain coordinates and size (the width/height above). By accessing built-in properties, these options came up automatically. I guess this is an instance that has been created....Reading on more, what specifies the coordinates and size is actually a type of method. OK....
As it gives more instructions of calling other methods, it mentions a key point here. Constants vs Variables.
Constants vs Variables
This is something that's always been pretty clear to me. Knowing from school - both as a teacher and learner, a variable is something that can change whereas a constant is something that must stay the same. Applied to a practical situation e.g. a Science experiment where the effect of sunlight on plant growth is being observed and measured. variables and constants are easily understood. The independent variable is the thing that you are changing, the dependent variable is what you are measuring and the constants are what must stay the same. In the plant example, we would be changing the amount of sunlight, measuring the length of the plant and keeping the same everything else: the amount of water, type of plant, soil, position of the plants, time of day to measure it...etc. etc.
So constants cannot be changed. They stay the same. However, they can be grouped together with related constants in coding - as part of enumeration.
textField.borderStyle = UITextBorderStyle.roundedRect
The roundedRect property is a constant - it cannot be changed but it is chosen from other potential options (also constants). Still not completely clear on functions, methods...
Functions vs Methods
In contrast to Constants vs Variables, this is a distinction I've never been that clear on. So it will be useful to find out the difference for sure! A function is defined outside of a type whereas a method is a group of statements defined within a type. Basically, methods are preferable because then they can be used and accessed by others. Functions are standalone.... Apparently an exception where functions are better to be created than methods is for functionality you want to apply over multiple classes that are not related.
That helps clarify a little...basically you want to create and use methods over functions, even though both use the 'func' keyword. I suppose that will become clearer with more experience!
Anyway, next time, I'm looking at 'Creating Custom Classes'. I know a little about Classes from before, how they bundle together values and functionality. It WILL be much sooner the next time I blog!
Comments
Post a Comment