Learning Swift — Days 108-110

Review of the Swift Programming Language

Day 108

Crazy, crazy day today… I can’t find the time to do extra things… I am mentally exhausted. Nevertheless… here is what I could review today:

  • Numeric Type Conversion
  • Type Aliases
  • Booleans
  • Tuples
  • Optionals
  • Error Handling
  • Assertions and Preconditions (I would like to see more practical examples of this)

Basic Operators

Day 109

New day, possibly a calmer one when I can stay and practice a bit of Swift finally. Here is what I could review today:

  • Terminology (unary, binary and ternary operators)
  • Assignment Operator
  • Arithmetic Operators
  • Compound Assignment Operators (check also this article)
  • Comparison Operators (very interesting the part on tuples)
  • Ternary Conditional Operator
  • Nil-Coalescing Operator
  • Range Operators (I had never encountered the One-sided ranges before and it was a nice refresher on the logical possibilities of the language)
  • Logical Operators

Strings and Characters

  • String Literals (in the Multiline String Literals subchapter it is interesting to refresh the idea of indentation for multiline strings so that the indentation of the closing quotation marks (“””) are the one dictating the rules). Also, the new Extended String Delimiters feature is quite interesting and I would like to see it in action sooner or later.
  • Initialising an Empty String (I had never seen an empty String initialised as String() but it makes sense).
  • String Mutability (this is simply variable against constants unless we are using Cocoa, which I guess is the macOS basic framework where we need to use either NSString or NSMutableString).
  • Strings Are Value Types
  • Working with Characters
  • Concatenating Strings and Characters (the .append method works when we want to append a character to a string!)
  • String Interpolation
  • Unicode
  • Counting Characters
  • Accessing and Modifying a String.
    • String Indices: startIndex accesses the position of the first Character of a String while endIndex the position after the last character in a String. index(before:), index(after:) and index(_:offsetBy:) are all useful methods. They are methods usable on any type that conforms to the Collection protocol (which includes Strings).
    • Inserting and Removing: the syntax of this part is quite hard to interpret and, personally, needlessly complicated.

Day 110

I have very little time again today so I will try to maximise my reviewing in the morning. Enough talk, already too much! Let’s get reading!

  • Substrings (very interesting the part on memory management)
  • Comparing Strings
    • String and Character Equality: pay attention to the meaning of canonically equivalent. I’m not understand what it means: “String and character comparisons in Swift are not locale-sensitive.”
    • Prefix and Suffix Equality
  • Unicode Representations of Strings: this was quite hard, especially because of the terminology. I did not expect to need a degree in linguistic to be able to understand this.

Collection Types

I love this image from the Swift Programming Language — Language Guide
I love this image from the Swift Programming Language — Language Guide
  • Mutability of Collections: it is good practice to create immutable collections in all cases where the collection does not need to change. Doing so makes it easier for us to reason about our code and enables the Swift compiler to optimise the performance of the collections we create.
  • Arrays: very interesting the Creating an Array with a Default Value subchapter.
    • This is most interesting: “You can also use subscript syntax to change a range of values at once, even if the replacement set of values has a different length than the range you are replacing
    • Remember to use .removeLast() instead of .remove(at:) passing it Array<Element>.count - 1.
    • Iterating Over an Array, interesting points:
      • If you need the integer index of each item as well as its value, use the enumerated() method to iterate over the array instead. For each item in the array, the enumerated() method returns a tuple composed of an integer and the item.
  • Sets: I will pay someone a good ice cream if they will find me a resource that teaches programming all the while explaining Hash Values and Hashable and all concerning Hash(ing) like I’m 5!
    • Adding items is done with the .insert(element) method.
    • Sets are not ordered but they can be ordered for iteration using the .sorted() method.
  • Performing Set Operations
    • Fundamental Set Operations:
      • Use the intersection(_:) method to create a new set with only the values common to both sets.
      • Use the symmetricDifference(_:) method to create a new set with values in either set, but not both.
      • Use the union(_:) method to create a new set with all of the values in both sets.
      • Use the subtracting(_:) method to create a new set with values not in the specified set.
    • Set Membership and Equality
  • Dictionaries: we can add new items using subscript syntax instead of appending or inserting. The same syntax is used for editing the value associated with a key.
    • We can also use the updateValue(_:forKey:) method to perform the same operation. Just bear in mind that this method returns the old value of a key and not the new one.
    • To remove a value we either set a key’s value to nil or we use the removeValue(forKey:) method (which returns the old, removed value).

Control Flow

Here is a good description of the control flow statements in Swift:

These include while loops to perform a task multiple times; if, guard, and switch statements to execute different branches of code based on certain conditions; and statements such as break and continue to transfer the flow of execution to another point in your code.

Swift also provides a for-in loop that makes it easy to iterate over arrays, dictionaries, ranges, strings, and other sequences.

  • For-In Loops
    • I like this code example:
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
  • Very important is the distinction between the stride(from:to:by:) method which stops before the to: value and the stride(from:through:by:) method which includes the value stored in the through parameter.
  • While loops (beginning)

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

%d bloggers like this: