Posts

Showing posts with the label inout

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

Stephen DeStefano Swift 5 Course - Part 3 (Lectures 8 to 11)

Image
Well it's been a good few days but I'm back! No need for more of a preamble than that - straight into functions! Start Time - 14:06 Functions These are SELF-CONTAINED. It is best to name what the function actually does. Syntax - the name, parameters, return bit etc. Nothing else new here. Bit about the return value and print statement used. Underscore - placeholder for a constant/var without an actual name. 9 out of 10! Only one that was wrong was a bit of a trick question. OK next! Returning Multiple Values So this of course means returning a tuple All very technical - again, Stephen combines lots of elements - for loops, if statements, optionals...all hard to keep on top off! Names of return types - optional. Used to improve readability of code. Argument Labels and Parameter Names Argument label - used when calling the functions. Better for readability. Swift used the parameter name as the default. Labels are used on the outside - on the left of th...

Creating Custom Classes

I know that I promised to make my entry much sooner than this but, once again, life got in the way! So here we are, a couple of weeks later than planned. Anyway, it's the first time in months that I'm actually doing this blog alongside some coding. So it's a step in the right direction! Moving forward, after working through this book, I plan to do this book alongside another course or text, so hopefully the 'live' recording of it will be useful and purposeful.  This chapter is all about creating custom classes. Now I know, more or less, what a class is. From what I recall, a class bundles together data e.g. variables, constants, functions...all to then create specific objects or instances. We'll see what this chapter brings...! So the task in this chapter is to create a functioning calculator - able to add, subtract etc. class Calculator {      } This is how to set up the class. The syntax is have a capital letter - different to variables, func...