Swift Pair Programming – Session 8

I’ve been sick for a few days. Didn’t want to cancel pair programming with Nick. We met online at 6pm. I got access to a repo we were working on last time with GPS. Very glad we paired. Finally getting comfortable with Swift. Started in on the agenda and didn’t stop until 7:20pm.

I had a list of topics I wanted to cover last night:

  • Conditionals
  • Coalescing Operators
  • Optional Chaining
  • Guard
  • Defer
  • Unwrapping (forced vs implicit)
  • Typealias

Surprisingly, we were able to cover all of the topics last night. I’m excited because next time we’ll be dipping into xctests.

Here are my notes from the session:

Conditionals are either some() or none(). If they are none that means they are allowed to be nil. If they have a value they will return some() with the value inside of it.

Coalescing Operators allow us to set an enumeration with a default value. If no value is set for it, we will give it this default value

Optional Chaining allows you to code nested ifs. If the first item fails, none of the following nested calls will ever be called. Allows for elegant failing.

Guards are for hacky code. If fails, we will know it and react accordingly.

Defer executes code in a block whether the containing function’s block succeeds or not. I like to think of defer as a function’s deconstructor – use it as clean up.

Code in a defer is guaranteed to execute whether the following code in the method executes or not. We also verified that the defer block has access to variables in the containing method block’s scope.

Unwrapping signals to the compiler, “I know, compiler, this will have a value when this statement is called. It may not look like it now, but I guarantee it will have a value when it needs it.”

Forced Unwrapping differs from Implicit Unwrapping.

Forced unwrapping means that you create a conditional with a question mark. This means that you are telling the compiler that “this value might be nil. Don’t freak out, I know this can be nil.” Then when you use the conditional parameter, you MUST force unwrap it in the method block.

Implicit unwrapping simply saves you from having to force unwrap a conditional every time you use it in the method block. By writing a bang character in the parameter list in the function signature, you are telling the compiler, “I know this might look like it’s nil now, but I promise I will have something in it.” Implicitly unwrapping a parameter, saves time from having to force unwrap a conditional every time it comes up later in the statement block

Typealias is used to make your code more readable. That’s it. Let’s say you create a tuple that holds an int and a String. Let’s say you’re using this to hold a customer id and a string.

Now let’s say you have a tuple that saves an Int and a String to hold a business with a business id and a string. If you just had the datatypes (Int, String) and (Int, String) how would you tell them apart?

This is where a typealias comes in handy. By typing the tuple, you can refer to them with a name rather than have to determine each time you see a tuple with (Int, String).

For customers we could write:

typealias customer = (Int, String)

For employers we could write:

typealias employer = (Int, String)

Questions that came up during the pairing session:

  • Should we use a guard statement, a conditional, an implicitly unwrapped variable, or if let when dealing with potential nils?
    if let n = function_001(), let m = function_002() 
    
      // do something
    
      else 
    
      // return error code
    
      Assignment that may or may not succeed.
    
      Only expects argument

Shared Xcode Tips and Tricks:

  • Set Finder to always show scroll bars – System Preferences -> Finder -> Always show scroll bars
  • Use GPX files to spoof Pokemon Go game – Allows you to play out a GPX file, including location and speed, catch pokemon without walking to them.
  • Fold all code to reduce cognitive load while coding: Shift + Alt + Command + Left Arrow
  • Unfold all code – Shift + Alt + Command + Right Arrow
  • Enable Focus Follows Selection to know where your cursor is at all times – Editor -> Code Folding -> Focus Follows Selection
  • Update the comments in Xcode to show at a smaller font size – About -> Preferences -> Font -> Click on comments -> reduce font size

Questions for next time:

  • Are there any online Swift playgrounds?
  • Is ScreenHero invites open to anyone?
  • Should we start streaming these pair programming sessions on twitch?
  • Mind if I start screenrecording these sessions?
  • What’s the difference between a argument and a parameter?
  • Is there an Xcode plugin that allows finger pinching to scale the font size of the code?

Thoughts to meditate on:

  • Closures are like expressions. Think of it like a parameter, or a return, or a type.
  • Able to capture scope (I don’t understand this)
  • Start xctests next time
Advertisements

Author: David Neely

Professional Software Developer. Technology and Web Coordinator at the University of Hawaii's Manoa Career Center.