Cheatsheet Swift - Index

May 27, 2019
swift cheatsheet

As a developer from Android background, I wanted to do some flexing on some cross-platform development. I started development in flutter based on react architecture and hit some roadblocks as I have to jump to the native platform to achieve my specific goals. Very soon enough, I found myself keeping one foot on Android(Java, Kotlin) the other on iOS(Swift, Objective-C) while keeping my balance using Flutter(Dart). The only problem however was I am not an iOS developer. I used my logical understanding of code structures to translate what I want to accomplish into iOS code, but that’s not enough, I feel like there is some landmine that is buried somewhere that could explode on a slight misstep.

So I decided to do some decent study on iOS using Android as a reference. Yeah, you heard me right, “Android as a reference”.

First, if I need to develop a native application in Android, I either use Java or Kotlin. Similarly, if I need to develop a native application in iOS, I have to use either Objective-C or Swift. I choose to use whatever Swift specification is the latest at the time of this writeup as it seems to be the recommended way for writing applications in iOS.

I am a firm believer in the “Pareto principle.” the famous 80/20 rule. So in my case, I am going to use 20% of the language features to achieve 80% of the work. How I came up with that 20% is based on my experience and gut feeling. The 20% that works for me might not work for your use case and your problem domain.

From my understanding, programming languages can be divided into statically typed and dynamically typed languages. I come from a statically typed language background C, C++, Java and Kotlin, so I was happy that Swift was statically typed as well. However, unlike Java or Kotlin whose, memory management is taken care of by the JVM, in Swift, I have to rely on Automatic reference, counting something akin to Smart Pointers in C++.

I decided to construct a Cheatsheet that would help me speak Swift, as I don’t want to jump to Google each time I want to translate what I want to accomplish. I feel any programming language would have the following:

  1. Constructs to store data like numbers and text. Constructs to store collection of data using list, set and dictionaries. (link)

  2. Constructs to alter the logical flow within the executable program like selection or iteration. (link)

  3. Constructs to organise code to improve reusability, readability, extensibility and maintainability. (link)

  4. It is always painful to write everything from scratch, so ways of adding code contributed by others and ways to provide code written by you to others. (link)

  5. Finally, it is imperative to have an understanding of the memory model of the language. For languages like Java or Kotlin, you have a garbage collector to reclaim the memory when not used(even so when we are reckless we can make the GC believe useless memory as useful), but it is not the same for C or C++ where you have to be very careful about allocation and deallocation. Swift closely resonates with C and C++ in that sense and can cause memory leaks when not careful even with ARC. So I decide to prepare a Cheatsheet to remind me of that trouble. (link)

So with the initial groundwork done, let us try to use this as a reference point to work our way up. As we start moving into the actual domain, we would add new and better abstractions to accomplish what we want to the Cheatsheet at the same time keeping in mind to keep it small(Pareto principle) rather than creating a reference manual again.

comments powered by Disqus