Posts

Showing posts with the label value vs reference type

Ray Wenderlich Functions and Types Course - Part 12

Here we are! This will be to conclude Catie's course. It's been useful to go over the content. The issue is that none of it is project-based - I need to see more in context! The MOST useful way of using the info will be in the consolidation entry to follow this. So here we are, the rest on protocols and inheritance! Start Time - 12:56 Protocols and Extensions Extensions - I know about these already. Extending them to protocols... Using protocols you can say 'composition'. If class inheritance isn't quite working then protocols are the way to go! Extensions can't have stored properties or designated inits. At the moment I don't see the advantage of having extensions for a protocol when all of that could just be in one protocol. You can also extend native types e.g. Ints. This is quite advanced! extension Numeric {     var squared : Self { self * self } } An example of putting in member-wise init.  Challenge! Last one fo...

Ray Wenderlich Fundamentals Course - Part 5

Image
OK last part! I'm going to complete this fundamentals course today! Also, Xcode looks like it's ready for the official update - so I won't need Beta anymore. Anyway, time to go for it. Start Time - 20:44 Structures Grouping sets of related data together. Technically, all of the below are structs!   struct Student {          let name : String     let grade : Int     let pet : String ? } let bobStudent = Student (name: "Bob" , grade: 33 , pet: nil ) Most of this is recap so skimming through *Technical glitch for 5 approx minutes. Wifi back on! Mutating. I've made sense of this at last! If you do a function that alters one of the STORED PROPERTIES e.g. grade, then I have to use the mutating keyword. Cool.  Value type. I could clone an instance. That new version is unique and does not affect the original. Class (reference types) do.. It's also got to be a var, to change any of the va...

Lynda Course - Swift 5 Essential Thinking (Part 5)

Image
Right! Back on it after several days away. Still struggling to get into some sort of routine. So I'm glad that I've managed to create a little bit of time now - around 40 minutes. Need to go over some of the last bit on closures before the next section... Start Time - 17:14 Type Aliasing OK, it was this bit to go over! typealias AttackTuple = (name: String , damage: Int , rechargeable: Bool ) var sunStriker: AttackTuple = ( "Bruce" , 4 , true ) I totally get the benefits of type alias. There is no initial value given to the AttackTuple. But the var of sunStriker does.  func levelUpAttack(baseAttack: AttackTuple ) -> AttackTuple {     let increasedAttack: AttackTuple = (baseAttack.name, baseAttack.damage + 10 , true )          return increasedAttack      } levelUpAttack (baseAttack: sunStriker ) An example of how to use the AttackTuple as a type within a function....

Stephen DeStefano Swift 5 Course - Part 5 (Lectures 17 to 22)

Image
I'm back! So a good turnaround from the last entry! Yes, I'm going to make time! We're diving into classes and structures, which is always a useful comparison.  Start Time - 15:42 Syntax Ok so a good overview! Syntax I'm fine with so just a recap here. Properties - accessed with dot syntax. Dot syntax - can be used several times e.g. if there is a custom type within the class/struct, then the property of that is used. Sounds complicated but actually very simple! Initialisation -   A good definition! Stored properties cannot be in in an indeterminate state. That means they can't just be up in the air.... So either give default values within the body of the class/struct, or have them created upon the instance being declared. So if a property will always take the same initial value, then it's best to do it as a stored property. Makes more sense then having to init this every time! Value vs Reference Types Ah the age-old debate! In th...