iOS: Beginner’s Guide — UIKit (1/3)

John Kim
9 min readJan 17, 2021

Let’s start with the absolute basics:

iOS is the operating system that “operates” on iPhones and iPads. It’s responsibility is to be accountable of the rudimentary functionalities of an everyday phone like making calls or running your basic app from the App Store.

macOS is the operating system for Apple’s desktop, and it is like a fore-father of the other Apple operating systems such as the iOS operating system that we have discussed above.

watchOS is recognized as Apple’s smallest operating system.

Swift is the current go-to programming language that Apple uses to build apps for the operating systems above: iOS, macOS, watchOS, and others. It is important to note that the Swift does not include building user-interfaces, music, or extracting data from an API. All of these “extra-functionalities” are handled with applying existing frameworks such as UIKit, AVFoundation, and others.

The major hallmarks of Swift goes like this: Swift 1.2, 2, 2.2, 3, 4, 4.2, 5, 5.1, and 5.2. Swift 1.2 & 2 were the initial major updates to Swift, Swift 2.2 deprecated syntax introduced in Swift 2, Swift 3 made the language easier to write, Swift 4 transformed the language to make it more expressive, Swift 4.2 introduced various new features leading to Swift 5 which focused on making the language stable in terms of its longevity. Swift 5.1 was responsible for improving the SwiftUI framework. Now, from the time this article has been written, we have Swift 5.2 which builds on from Swift 5.1.

For frameworks, we have Apple’s toolkit for UI known as UIKit. This is responsible for helping you have access to creating buttons, textfields, and other UI-related components.

AppKit mimics UIKit except that it is for desktops, not iPhone or iPad.

WatchKit, as you can probably guess, mimics UIKit except that it is for watchOS. Though UIKit and AppKit share many similarities, WatchKit is much different and is considered to be simpler.

Cocoa Touch refers to the collection of Apple’s iOS frameworks, including UIKit. Others include SpriteKit for creating 2D games, SceneKit for creating 3D games, MapKit for maps, Core Graphics for drawing, and Core Animation for animations.

Cocoa refers to the collection of Apple’s macOS frameworks. It consists of AppKit, Foundation for basic functionality, Core Data for object graphs, and much more.

NextStep is the operating system created by Next, a company that Steve Jobs founded, and eventually NextStep technology was inserted right at the core of Apple’s development platform after Apple purchased Next.

The iOS simulator is an Xcode tool that works like a real device (iPhone or iPad) that lets you test your apps efficiently and quickly without access to a real Apple device.

Playgrounds are testing environments where you can test any code you might have on Swift and see instant, visible results. They are essential to learning, not building real apps.

Crashes is when your code fails. You will see a crash report on Xcode whereas if the app were to be live, the user would then be redirected back to the home screen.

For the first step of becoming an iOS developer, I would recommend being familiar with playgrounds. For those who are not well-familiar with playgrounds, you can access it by first downloading Xcode from the App Store. Then after you’ve downloaded the application, open it and the first window you should see (at least for the current version that I am working with) is:

I believe there was an option to start a playground right off the bat from this window, but I think they have changed it so what I do alternative is first click on “Create a new Xcode project” and you will see this massive window appear asking to “Choose a template for your new project:”. Just dismiss this by clicking on “Cancel” and the entire window will disappear.

You should notice that on the top navigation bar, it will still say “Xcode” and by clicking on “File” then “New” then “Playground”, you should see this window appear:

If you are first starting out on Playgrounds, you don’t really need to be focusing on the template that you first start out with so I just recommend “iOS” then “Blank” to start out with a fresh, new slate.

You will notice that when you are attempting to run your first lines of code on Playground, it will take a bit more time at start than every subsequent attempt and that is due to Xcode launching a mini simulator behind the scenes at the very beginning.

There is a notable split between the left and right side of the Playground, and the left side refers to the code panel whereas the right side refers to the result panel. You can think of it like a cause and effect relationship where your code represents the cause, and after writing lines of code, you would naturally expect an effect or result on the right side or panel.

“//” represents commenting, which typically helps you understand your’s or any other developer’s code later on when you or he or she decides to revisit it.

Variables and Constants

A variable is data that stores a value that can change over time.

A constant is data that stores a value that cannot change over time.

Some of the benefits of using a constant over a variable is that 1. Xcode informs you when you make an error when attempting to change a value that originally is a constant and 2. using constants apply optimizations to allow your code to run faster than if you were to use a variable.

“var” is used to declare variables and “let” is used to declare constants.

It is conventionally great practice to be using constants over variables whenever possible to make your code easier to understand. It is also important to note that throughout your code, variable and constant names need to be distinct or unique. In other words, they cannot share the same name.

Data Types

  1. A “String” is a string of characters.
  2. A “Int” is an integer or round number.
  3. A “Float” stores numbers with a fractional component with limited space.
  4. A “Double” stores numbers with a fractional component like a “Float” but with the highest or double accuracy. Doubles still do have limits but stores more allocating space than Floats.
  5. A “Boolean” stores whether a value is true or false. It is short for Bool.

A “type annotation” is the concept where you inform Swift what data type your variable or constant will store later on instead of at the moment.

A “type safety” is the concept where you declare a data type for a variable or constant at first and try to then store a value of a different data type in that variable or constant, ultimately resulting to an error.

A “type inference” is the concept where Swift can infer what the data type of a value is for a constant or variable. In the case of a number with fractional components like “50.67”, unless if a data type is specified in-advance, Swift will always infer that this is a Double not a Float.

It is coding convention to have data types specified in upper-case where names of constants or variables are in lower-case. Another coding convention is the phrase “camel case”, which means that the initial letter of the name of a variable or constant should be lower-cased and the beginning of the subsequent word attached should be upper-cased. For example, instead of typing “camelcase”, it would be “camelCase”.

There are 3 ways of how a variable or constant can store data:

A. You can simply assign a value to the variable or constant.

B. You can use “type annotation”.

An example of “type annotation” would be the following:

C. You can specify a data type and value at the same time like the following:

var number: Int = 5

Operators

  1. “+=” indicates to add then assign to.
  2. “-=” indicates to subtract then assign to.
  3. “%” is the modulus or modulo, which indicates the remainder when you divide the left hand number with the right hand number evenly.
  4. “>” indicates greater than.
  5. “>=” indicates greater than or equal to.
  6. “<” indicates less than.
  7. “=” gives a variable or constant a value.
  8. “==” is comparing two values to check if they are equal to each other.

NOTE: When comparing two Strings in Swift, strings are case-sensitive.

9. “!” is the “not” operator, indicating the opposite or reverse of what it is attached to.

10. “!=” indicates not equal to, which is the opposite of “==” or equal to.

String Interpolation

String interpolation is typically used when trying to place a value of a different data type than a String inside of a String.

It can also be used to combine Strings together using a “+” operator, but you cannot use this operator to combine different data types other than a String to a String.

Whatever is in between the “\(example)” can even contain an expression in Swift like so: “\(example * 10)”.

Arrays

Arrays group various values together into a single collection, and those values can be accessed by their position in the collection.

An item or object’s position in an array is called its index, and it begins with 0, counting incrementally upward to 1, then 2, then so on and so forth.

You can check the data type of any variable, constant, or array by using the following function: “type(of: variableName)”.

If you want the array to contain a variety of data types instead of one uniform data type, then assign the array with the “[Any]” data type. Be-careful of assigning a variable or constant with a data type of an array and not assigning the variable or constant with a value. This is a mistake because you are not telling Swift what array to create, but instead what the array data type will be.

There are 2 popular approaches of creating an empty array:

  1. Assign an empty array as the value: var emptyArray: [Int] = []
  2. Use the constructor “()”: var emptyArray = [Int]()

There are a limited set of operators you can use on arrays like the “+” and “+=” operator.

Dictionaries

Dictionaries, like arrays, group various values together into a single collection except that the difference lies in how they access the values in their collection. Arrays are index-based, whereas dictionaries are key-based.

The format of a dictionary is a key, then a colon, then the key’s value. You can access the key’s value by writing the name of the dictionary and then attaching a set of brackets “[]” and within the brackets, insert the key like so:

“dictionaryName[key]”

Dictionaries can store a variety of data types, but its keys typically consists of strings.

Conditional Statements

  1. If and else statement: a statement where you give Swift a condition to check and if the condition is true, the block of code is executed. A block of code just refers to the chunk or literal block of code indicated with an open brace and close brace “{“ and “}”. “else” executes a block of code if the condition is false, or “else if” adds more separate conditions to the statement if necessary.

The open and close brace are often referred to as the “curly brackets”.

You can use “&&” or and to check if multiple conditions are all true.

Swift adopts short-circuit evaluation where it checks if the first condition of multiple conditions is true and if it not, then it does not even bother to evaluate the rest of the conditions to improve performance.

Loops

Loops are programming constructs that cycles a block of code repeatedly for as long as condition is true.

NOTE: For debugging purposes, if you want to print a value or text, you can using the “print()” function.

  1. The first type of loop is the “for in” loop.
  2. The second type of loop is the “while” loop

The closed range operator is three periods in a row “…”. The number on the left hand and right hand side of the “…” are both included.

If Swift doesn’t have to use the constant component of the “for in” loop, then you should replace the conventional constant “i” to “_” for optimization.

The constant is handy to index into an array.

Another closed range operator is two periods and a less than operator like so: “0 ..< 2”. In this case, this means including 0 up to but excluding 2.

NOTE: To count how many items are in the array, you can use the “.count” method. Conventionally, programmers use loop constants like “i”, “j” or “k”, but it is up to you on how you want to name them.

The “while” loop repeats a block of code until you inform it to stop. There are 2 keywords:

  1. “break”: used to exit a while or for loop at a point you want.
  2. “continue”: only exits the current iteration of the loop. It will jump back to the top of the loop and continue from there.

NOTE: If you don’t stop your while loop, it will lead to what’s called an “infinite loop” where your code runs infinitely.

--

--

John Kim

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com