Learning Swift — Day 199 to 200

Day 199

Yes, I reiterate, I fell again into Paul’s alluring trap and I’m following the 100 Days of SwiftUI. And you know what? I’m enjoying it!

Today I’m doing Day 13, which you can find at this link.

I have also built and saved the two tutorial articles for today’s Swiftoberfest!

Time to go to bed, now, it has been such a long day, teaching from 1 to 9.30pm non-stop…, I do not know where I found the energy to practice Swift at 11pm!

Day 200

This is a great day as it marks the 200th day in which I was studying Swift since February 1st when I started the first 100 Days of Swift challenge! I had many ups and downs, but I managed to stay focused and to keep up with the learning process!

To celebrate this I will be running a promotion on all my published musical scores and books: if you purchase any one complete work (that is, not a single separate part) you will get any lower priced one other for free (this is to prevent geniuses from buying the only 1.99$ product and asking for the 20$ one for free, I would really like to trust human kind otherwise)!

All you need to do is contact me on the Facebook page for my publishing work or via the various e-mail forms you can find on any of the websites where I sell my music. For convenience, I am writing down here all the necessary links:

The promotion will run from today up until I get back from Pragma Conference next Saturday. Please understand that I may not be able to send you the free book until then but if you do not hear back from me by the Monday after that, definitely write me again, insist!

Thank you very much for your support!


100 Days of SwiftUI — Day 13

This is the first of three Consolidation Days, those same ones I hated as much as they made me grow in the first 100 days! Let’s see what I can do with this!

The first review topic is about Variables and constants. This 3:41 minutes video was pretty clear: variables are created to store values that can change at a later stage while constants are created to store values that we want to never change afterwards. If we try to change a constant (let) after its creation we will get back a compile-time error. Same, if we try to create a new piece of data using the same name, we will get an “invalid redeclaration of #name” error.

The second review topic is Types of Data. Here we review the fact that Swift doesn’t let us write data without being absolutely clear about what type that data will be. We either have to use type annotation, that is writing the name of the type after a colon following the name of the property or assign a value directly, which will use Swift’s built-in type inference. We also meet Double and Float, two ways of representing numbers with a fractional components but with a different precision and Booleans, which represent absolutes (true and false). Finally, we get acquainted with the convention of writing names using camel-case.

The third review topic is Operators and here we start by meeting the usual math operators: + for addition, - for subtraction, * for multiplication and / for division. Using this after a variable will give a result but will not assign it back to the variable itself. To do so we have to add an = sign after the operator. Then we have the modulo operator % which performs a division between the two operands and returns the remainder (e.g.: 10 % 3 = 1). Comparison operators are next in line, with the usual > greater than, < less than, >= and <= for adding equality, then == and != to check for absolute equality and inequality. The not operator ! also flips the value of Booleans.

The fourth review topic is String Interpolation, which lets us combine variables and constants inside a string. The syntax for this is \(nameOfVariableOrConstant). Inside those parentheses we can also perform mathematical operations.

The fifth review topic is Arrays. Arrays let us groups lots of values together into a single collection. The syntax for arrays is [value1, value2, value3]. Arrays start counting at 0, so the first element will be array[0]. Normally all values in an array are of the same type but if we really want we can make an array of Any to circumvent this. To know what type an array is we can use the type(of:) method.

The sixth review topic is Dictionaries. They are collections like arrays but they let us access their stored values using keys instead of indices. The syntax for them is [key1: value1, key2: value2, key3: value3] and we can access them using dictionaryName[key1]. This will return an optional value because that value may not exist!

The seventh review topic is Conditional Statements which in this case is limited to if, if else and else. We also meet the && (binary AND operator) that uses short-circuit evaluation to increase performance. So, if something is true the code inside the braces will execute, otherwise it won’t and the code execution will continue after the first set of braces.

The eight review topic is Loops. The for-in loop lets us repeat an action several times, using either the closed-range operator (...) or the half-open range operator (..<). We can either use for i in if we are going to use the i index, or just for _ in, if not. We can use whatever we want instead of i. The while loop is another kind of loop that checks for a condition and repeats the action inside the brace until said condition becomes false. We can use break to interrupt execution of the loop or continue to skip the execution of the current iteration of the loop.

The ninth and last review topic for today is the Switch case, which is another control flow tool. Instead of checking for a condition, we check for the value of a property. We use switch property then some values preceded by the case keyword and followed by a : and the code to execute. If our cases do not cover all possible values, we need to use a default case.


I will now take a break and analyse the two Swiftoberfest articles from yesterday.

The first one is this and it covers Machine Learning. This was very interesting but also quite obscure as I have no idea what all this could be used for. I should study the NaturalLanguage framework when I get some time…

The second article is this one, on how to make a fixed Spacer in SwiftUI.

You can find the playground and files for this day of study here.

100 Days of SwiftUI — Day 14

We start from here.

The first review topic is Functions! Functions let us write pieces of code that we want to be reusable later on. The syntax is func functionName(argumentName? parameterName?: parameterType) -> returnType?. I used the question mark to underline the fact that a function may not have any parameter/argument name or return type. The argument name is the name used externally, when the function is called, while the parameter name is the one used internally.

The second review topic is Optionals! This was a mammoth video, 17 minutes long, and with reason, because optionals are such a fundamental part of the language. The syntax for optionals is to put a ? after the type that needs to become optional. Such a value cannot be used, though, until it gets unwrapped, using if let (or guard let) syntax. Paul goes at length to explain this concept to us in three different ways and I suggest you go on reading or watching his lesson. We meet here the exclamation mark operator, which lets us tell Swift that we are sure we will have a value (accepting the risk that our code will crash in the end!). An implicitly unwrapped optional is a trickier thing: we say to the compiler that such a piece of data will have a value and the compiler will trust us. If it won’t have it, your computer will just burst! No, it won’t… but Xcode will complain, even if sometimes the two things coincide!

The third review topic is Optional chaining. This feature allows us to check for multiple optional values (or properties of values) one after the other and let Swift check if there is a nil there or not. The syntax for this is a?.b?.c, where c will be reached only if a and b are non-nil. We also meet the nil coalescing operator, which provides a default value after ?? to use if the value before the question marks is equal to nil.

The fourth review topic is Enumeration, one of my favorite data type and one I am using extensively in my app building because it lets me be super-flexible! Their syntax is enum EnumName (singular!!) { case case1name, case2name }. We can add additional values to cases like this: case caseName(propertyName: propertyType) and therefore have incredibly fine-grained control over our data. Enumerations are perfect for usage with switch cases.

The fifth review topic is Structs and this video was surprisingly short, seen how many things we should know about structs. Anyway, they are complex data types that can contain properties (var and let) and methods (func). Creating a copy of a struct creates a new instance completely, changing which doesn’t affect the other.

The sixth and final review topic for today is Classes, a big beast! They are very similar to structs but they do not have a memberwise initialiser so we either make their properties optional, give them default values or write a custom initialiser. This last one is usually the best option and is done via init(propertyName: propertyType) { } syntax. We can add methods to our classes and, most important of all, we get class inheritance, that is, we can build a class atop another one and so on. Each subclass can expand on its superclass functionality, provided it creates a new initialiser with each new added property. Be careful, though: classes are copied by reference, which means that every copy points to and modifies the same object in memory.


Time for the second and probably final break of the day, with the next two articles from Swiftoberfest!

The first article is titled “How to decode JSON from your app bundle the east way”. I will try and apply this extension to my app as soon as possible.

The second article is on SwiftUI and is titled “How to detect the Reduce Motion accessibility setting”.


That’s it for today. Once more, you can find all the files I worked on today here.

If you like what I’m doing here please consider liking this article and sharing it with some of your peers. If you are feeling like being really awesome, please consider making a small donation to support my studies and my writing (please appreciate that I am not using advertisement on my articles).

If you are interested in my music engraving and my publications don’t forget visit my Facebook page and the pages where I publish my scores (Gumroad, SheetMusicPlus, ScoreExchange and on Apple Books).

You can also support me by buying Paul Hudson’s books from this Affiliate Link.

Anyways, thank you so much for reading!

Till the next one!

Published by Michele Galvagno

Professional Musical Scores Designer and Engraver Graduated Classical Musician (cello) and Teacher Tech Enthusiast and Apprentice iOS / macOS Developer Grafico di Partiture Musicali Professionista Musicista classico diplomato (violoncello) ed insegnante Appassionato di tecnologia ed apprendista Sviluppatore iOS / macOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: