Posts

Showing posts with the label map

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 Course - Second SwiftUI App Part 1

Image
Here we are now! Haven't been able to code for a few days. A quick entry here - around 20 minutes, then some proper time tomorrow! Rather than play around with the SwiftUI view stuff, I feel like cracking on with another Ray course. There's plenty of content on RW.com so I want to get stuck in! *Entry ends up going over two days! Start Time - 12:01 I struggled with this 'To-do' app before. I want to have another crack with it as it looks updated with UI even though the overall rating (3.4 out of 5) isn't great. The dude's name is Jessy Catterwaul. He looks a little like the Joker! There will be challenges etc. along the way. Models A point about copyright statements - something to bear in mind if/when working for a specific company. A bit about copying a p-list into the main folder. Done. Models - created a Swift file. This is where the data model will go. Another Swift file created with a class. OK challenge time - nearly there for now! cl...

Treehouse Intermediate Course - Part 12 (Closures 4)

Image
Right so I've connected to my personal hotspot and am going to code on the coach! I'll aim to do around an hour. Continuing with map, let's go! Start Time - 10:33 Flat Map Oh an example about blogs. How apt! So far, all clear - struct Post {          var post: String     var tags: [ String ] } let blog = Post (post: "Hi all!" , tags: [ "first" , "newbie" ]) let secondBlog = Post (post: "Now a bit about me!" , tags: [ "personal" , "info" , "details" ]) We are going to do something to combine these blogs I reckon! OK forget the above - I got confused! let blog = [      Post (post: "Hi all!" , tags: [ "first" , "newbie" ]), Post (post: "Now a bit about me!" , tags: [ "personal" , "info" , "details" ]) ] let tags = blog . flatMap { $0. tags } And extension time on the array agai...