Posts

Showing posts with the label closures

Ray Wenderlich Functions and Types Course - Part 6

Two days in a row baby! We're onto enumerations first. I want to get a couple more entries this week to finish this course, consolidate then explore more on rw.com. Start Time - 18:50 Intro Enums take advantage of type safety - options to choose from. They are value types. Enums Mostly recap here. Case syntax, raw values. Own raw values can be put in. enum Month : Int {          case january = 1 , february , march , april , may , june , july , august , september , october , november , december } let month = Month . january func monthsUntilChristmas (from month: Month ) -> Int {          Month . december . rawValue - month. rawValue } enum Season : String , CaseIterable {          /// ❄️     case winter          /// 🌾     case spring          /// 🌞     c...

Ray Wenderlich Functions and Types Course - Part 5

Image
No need for any pre text, let's go! The aim is to complete this chapter on closures today.  Start Time - 17:05 Filter, Reduce and Sort So filter can be used - having certain criteria rather than the for loop! Xcode updating so following along without typing at the moment. Another example with grades. This is what we had before - // -------------------------------------- var stock = [ 1.50 : 5 , 10.00 : 2 , 4.99 : 20 , 2.30 : 5 , 8.19 : 30 ] // -------------------------------------- let stockSums = stock . reduce (into: []) { (result, item) in     result. append (item.key * Double (item.value)) } So this is another use of reduce - complex stuff but good practice! You basically don't need to create your own sorting algorithms - this is what the closure does.  Sort Sorting an array of strings -  names . sort () names . sort { (a, b) -> Bool in     a > b } name...

Ray Wenderlich Functions and Types Course - Part 3

Image
Here we go! Getting back into a routine with this! Straight on to continue closures... Start Time - 19:45 Closure Syntax Writing closures inline is one of the main benefits. Using shorter syntax can make them easier but can also make it more complicated to figure out... This old chestnut I've seen before. let longClosure = { (a: Int , b: Int ) -> Int in     return a * b      } longClosure ( 4 , 3 ) let noParameterTypes = { (a, b) -> Int in     a * b      } let noReturnTypes : Operate = { (a, b) in     a * b } let shortClosure : Operate = { $0 * $1 } noParameterTypes ( 4 , 3 ) noReturnTypes ( 4 , 3 ) shortClosure ( 4 , 3 ) The difference is that there was a type alias with the closure signature there.  Here is a point about trailing closures -  // -------------------------------------- func printResult ( _ a: Int , _ b: Int , _...

Ray Wenderlich Functions and Types Course - Part 2

Image
Back on it! Two entries is the plan today - half an hour now and then again later - so as one blog. Let's go! Start Time - 13:01 Advanced Parameters Variadic parameters - So this can be an unspecified number of parameters. E.g. in the print function - could be one or more things to print! Here is an example - func getBestGrade (for grades: Int ...) -> Int {     grades. max () ?? 0      } getBestGrade (for: 4 , 7 , 1 , 8 , 19 , 3 ) This is different to what I assumed. I thought it would be an array.  Inout parameters func incrementAndPrint (value: Int ) {     value += 1     print (value) } Now the issue here is that the value parameter is a CONSTANT by default. It is immutable! So to fix that we need the inout keyword... func incrementAndPrint (_ value: inout Int ) {     value += 1     print (value) } OK a couple of things here. It only w...

Lynda Course - Swift 5 Essential Thinking (Part 4)

Image
Am smashing through this course and enjoying it! Probably because it's mostly recap and consolidation. Anyway, got a bit of time before a brunch today so no time to waste! Start Time - 11:26 5. The Wide World Of Functions So a point here is that a lot of these are already built into Swift. But we can create our own of course! Basic functions All good recap here. Overloading functions Heard of these once before in Pasan's story course. So this is using the same name for multiple functions, as long as their signatures are unique. // Base function func attackEnemy() {     print ( "Attacking enemy..." ) } // Overloaded functions func attackEnemy(damage: Int ) {     print ( "Attacking enemy; \ ( damage ) caused" ) } func attackEnemy(damage: Double , weapon: String ) -> Bool {     if damage > 8.4 {         return true     } else {         return false ...