Swift vs JavaScript

Image of Robbie Gay
Robbie Gay


Monthly Update

This past month, Anna and I adopted two cats, I played my first game of disc golf (and fell in the mud), we visited friends and family in Louisville, we made homemade pizza, and I hit some January resolutions (but not others).

The Job Hunt

Around Jan 15th, I started doing freelance iOS development for Tommy Warner. Tommy previously worked as a physics teacher at Lafayette High School for 6 years, during which time he taught himself to write software and started taking on clients through UpWork. After the 2018-19 school year, Tommy was able to step away from teaching and pursue freelancing full-time. Tommy told me that he had gotten to the point where he was having to turn away clients because he was too busy, and was therefore hoping to hire someone part-time to help write some Swift code.

Switching gears from web to mobile development presented some challenges. During the Bootcamp, we slowly ramped up from the most basic to more advanced apps. This meant that I usually got a fairly solid grip on a concept before adding something new. Working with Tommy, I had to jump into the middle of a project and I felt as though I was confused as to what was going on outside of the small scope of my current work.

To help remedy this, I spent some time last weekend and this week reading through the Swift documentation and taking notes. In addition to note taking, Xcode also has a cool playground feature that allows you to write code and see instant results — this allowed me to apply new concepts as I was learning them.

This past week I built my first personal app, a calculator! I was really excited to build something that I could run on my phone. While web development seemed challenging, there are online website builders and it didn’t seem impossible to build my own website, even prior to the Bootcamp. App development, however, had a certain magical quality and I was really excited to build something that I could run on my phone.

A demo gif of my calculator app
My calculator app!

We built a calculator app in the Bootcamp, so this project mostly involved refactoring my old code over from JavaScript to Swift. Even so, there were some problems (mostly WET code) that I was able to address in this build. I was also pretty happy with the retro color scheme. Since almost everyone has a calculator on their phone, I also added an Easter Egg — Party Mode — that can be accessed by inputting my birthday: “32193” (March 21st, 1993).

Swift vs JavaScript

I wanted to end this post with my thoughts on some key differences between Swift and JavaScript. Swift is for native iOS projects, and is based on Objective-C. Below are some key differences that I found:

  • Swift doesn't require ; to end each line — they are optional, and can be used to separate two short clauses on the same line,
  • () are also optional around things like if statements
  • In JavaScript, you use const to declare a constant and let or var to declare a variable. In Swift, you use let to declare a constant and var to declare a variable.
  • Functions are written a little differently in Swift: func funcName(argumentName parameterName: String) -> String { func code }.
    • To declare a function, you use func instead of function.
    • The function's name is similar to JavaScript -> funcName.
    • You can define the function's parameters one of two ways: named or unnamed. Named parameters have an argument name that comes first. In the above example, this means that when we call funcName we would need to specify the name of each parameter we give it: funcName(argumentName: "Some value"). The parameterName is the value used within the body of the function.
    • After the argument and parameter names you see : String. This is another interesting difference between Swift and JavaScript. You declare variable types using a semicolon followed by the variable type (things like: String, Int, Double, Bool, Array, or Any). Here we are declaring that the parameter type supplied will be a String.
    • Finally, after the parameters, we see -> String. This specifies a return type. Functions in Swift can either be Void or return a single value (of which you could combine several values in a single, compound tuple). Here we are specifying that our function will return a String value.

Reflection

I've really enjoyed learning Swift and like both building apps and freelancing in general. That said, I am still continuing my job hunt and have been sending out applications every week. For now, I plan on continuing to make more apps, both for clients and myself.