Posts

Showing posts with the label parameters

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

Treehouse Intermediate Course - Part 10 (Closures 1)

Image
Yes! So completes the aim to get four consecutive days! I actually enjoyed generics more than I thought - the concepts were so difficult but it was interesting finding out about them. Now onto closures - the area of Swift that I find hardest of all! This is apparently a 146 minute course, so I'll likely need at least 5 or 6 entries for this. 24 separate steps, so yes - similar to generics in terms of length! After that, the other iOS intermediate track (there is some crossover there with the Swift one), has a few other projects and bits. So that will be next! OK, let's go! Start Time - 12:22 Functions as Data A key point already is that closures are not a type. It is more that they are 'anonymous functions'. Simple functions - a recap! func printString( _ string: String ) {     print ( "Printing the string passed in: \ ( string )" ) } printString ( "Jeff" ) We can create a constant/variable and assign a function to it. No ar...