What I know about Swift - Part Three
This is likely to be the last post about my understanding of Swift - at this point in my journey. After this, thoughts and reflections will be "live", in the sense that they will be specifically about what I have just discovered, am trying to make clearer to me, or feel compelled to share. As before, I am not claiming that ANY of this information is technically correct or accurate; it is what I understand about Swift, at present!
Enumerations and Switch
I've lumped these together on the premise that Switch statements can be used within Enumerations; both use cases so there is a link there. Switch could actually have been part of the Conditionals - if/else etc.
Essentially Switch acts the same way as if/else, except it seems to be better for if there are numbers and multiple options, probably not best if the statements are complex.
The Enumerations are a way of having various cases for something, usually in some sort of order or sequence, though not necessarily.
You can also do the Switch statement with this enum.
Enumerations and Switch
I've lumped these together on the premise that Switch statements can be used within Enumerations; both use cases so there is a link there. Switch could actually have been part of the Conditionals - if/else etc.
var score = 54
var comment: String = ""
switch score {
case 0...20:
comment = "Good score!"
case 21...50:
comment = "Great score!"
case 50...100:
comment = "Excellent score!"
default:
comment = "Terrible score"
}
Essentially Switch acts the same way as if/else, except it seems to be better for if there are numbers and multiple options, probably not best if the statements are complex.
The Enumerations are a way of having various cases for something, usually in some sort of order or sequence, though not necessarily.
enum Compass: String {
case North, South, East, West
}
You can access the values e.g. compass.North
let newCompass = Compass.North
var compassComment: String = ""
switch newCompass {
case .North:
compassComment = "You are heading north"
default:
compassComment = "You are not heading north"
}
The default option is essential as it does whatever values are left - in this case, North has not been headed to! Obviously, there is a lot more that you can do with this, with multiple options etc.
Functions
This is a huge area of Swift. Some are already built in e.g. 'print'. The syntax when using a function is to use the () brackets. e.g. print("Hello") - brackets needed.
Functions can be created for a number of things - could be for calculations with numbers, for returning some combinations of strings...they can be used within all sorts of code.
Functions
This is a huge area of Swift. Some are already built in e.g. 'print'. The syntax when using a function is to use the () brackets. e.g. print("Hello") - brackets needed.
Functions can be created for a number of things - could be for calculations with numbers, for returning some combinations of strings...they can be used within all sorts of code.
func doubleNumber (number: Int) -> Int {
return number * 2
}
doubleNumber(number: 3)
A very simple example. You can have functions within functions (nested). Parameters are what goes before the arrow - you can have external and local ones here. You don't have to have a return, but the arrow means a return is expected.
Methods are a type of function. I think that they are called Methods because when functions are used within structures or classes (see below) that they become a type of Method. Not entirely sure!
Structures and Classes
First of all, let me outline the key differences between these. Classes 'inherit' properties when a 'subclass' is created. You can't do this with Structures. Classes also need explicit 'initializers'.
struct Person {
var name = "Josh"
var age = 32
var male = true
}
let newPerson = Person()
newPerson.name
let newPerson2 = Person(name: "Cersei", age: 42, male: false)
newPerson2.age
So Structures and Classes bundle together properties and values that can then be accesses. There is a lot more to this - functions can be used within these (methods) as can various other aspects of Swift that I've mentioned in this and previous blogs.
As described, Classes use 'inheritance', which means you can create a class, then a subclass without having to put in the properties all over again.
class Animal {
var legs = 0
var fur = true
var information = ""
init(legs: Int, fur: Bool, information: String) {
self.legs = legs
self.fur = fur
self.information = information
}
}
I'm not so confident with the syntax for creating subclasses, in terms of the override functions, super.init and so on. I'm hoping that when I start applying the information to actual apps and tasks that it will make more sense.
So that's it! There are several other bits and pieces that I could mention but nothing that seems worth mentioning.
Next time, I'm going to blog about my 'live' thoughts and feelings to sections of the new Ebook 'Learn to Code in Swift 4' by Kevin McNeish that I've just purchased. Hopefully, now I can draw a line under what I know, how I've gone about it and the reasons for learning Swift. From this point my journey should be clearer and more relevant with my immediate ramblings of what I am learning!
Comments
Post a Comment