Posts

Showing posts with the label Structure

Protocols in Swift - Part 1

So I got something wrong - the next course in my 'Beginner IOS' track is Protocols, not making an app! Not sure how that happened. Anyway, I'm not sure what Protocols are, but I assume it's do with more complex methods. Here we go.... What is a Protocol? Pasan uses the coffee shop example. The concept is to define a blueprint of methods, properties and other behaviours. Expected behaviours. protocol FullNameable {          var fullName: String { get }      } So that's the syntax for protocols. Whatever uses this protocol must use the fullName property and apparently it is 'gettable'. Not sure what that means...yet.... OK, we can read the property but not set it until we get an initial value....still not clear! Creating a Protocol struct U ser: FullNameable {      } An error comes up here as I have not put in the necessary properties. struct User: FullNameable {     var ful...

Object-Oriented Swift - Classes

Yep, two in a day - I couldn't resist! A great sign that I want to work through more coding! I'm picking back up with where I got to - from structures to classes... Introduction to Classes The syntax for these is similar in terms of the UpperCamelCase etc. A variable is used as a property rather than a constant because this value is likely to change. One of the differences with structs and classes are that we almost must write the init methods in the latter. Initializer methods are always just init, no need for prepositional phrases etc. class Enemy {          var life: Int = 2     let position: Point          init (x: Int , y: Int ) {         self . position = Point (x: x, y: y)              }          func decreaseLife(by factor: Int ) {                  life -= f...

Object-Oriented Swift - Complex Data Structures

I am now onto the next course! This is the last full course I need to recap/redo before continuing with the one I got to before. All of the skills I have built up over the past week or so have really made me feel positive about Swift. The next lot of videos are about 'structures' - bundling together a number of variables and values. It is going to be a step up.... Introduction to Structs The first example Pasan gives is vaguely familiar - it's about using coordinates for shooting towers for a game. The number of values needed means that we would need to create a struct. Creating a separate value for each of the x and y axes are useless. Creating a tuple would be better as the x and y values are linked together.  let coordinate1: (x: Int , y: Int ) = ( 0 , 0 ) This is fine but is going to be quite limited. Instead we are going to create a custom struct. As struct can define variables or constants, which are called stored properties. No value needs to be ass...

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. 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!" ...