Posts

Showing posts from January, 2020

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 4

Wanted to code at the weekend but had literally no time to! So a quick 20 minutes now, if that! Start Time - 17:38 Two more handy methods - compact map and flat map! Compact Map and Flat Map Compact - helps us deal with optionals. Iterates through each element then add resulting values if they are non nil. Flat - multi dimensional array - combine arrays into 1! This is how you would use a for loop to go through an array and convert what you can to an int - var arrayForValidInput : [ Int ] = [] for input in userInput {     guard let input = Int (input) else {         continue     }     arrayForValidInput . append (input) } But of course compact map is much better! let validInput = userInput . compactMap { (input) in     Int (input) } Flat Map  let arrayOfDwarfArrays = [   [ "Sleepy" , "Grumpy" , "Doc" , "Bashful" , "Sneezy" ],   [ "Thorin" ,...

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