Category: Playground


Swift Basics


Swift is the primary language used to build modern iOS apps. The current version of the language is Swift 5. With ABI and module stability in place, the code you write today will be valid in future versions of Swift. With SwiftUI we are entering a whole new paradigm for building iOS apps.

Don’t worry if you don’t understand what I’m talking about. You will later, but for now, the important takeaway is this; There’s never been a better time to learn Swift.

Where to start

There are a few key components to Swift to get started.

  • Variables
  • Operators
  • Collections
  • Functions
  • Flow Control
  • Classes
  • Objects
  • Inheritance

There is so much more in the language but these are some of the most common tools that you will use. Once you learn the basics the rest will make more sense. You’ll have a good understanding of the Swift language and all the tools you need to make swift apps.

Playground

I highly recommend that you follow along in a Playground. Learning can be much more impactful when you take action. Manually type in the examples and play around with it for a while.

If you don’t know what a playground is in Xcode, it’s really simple to get started. If you haven’t already, you’ll need to install Xcode. Then, when you launch Xcode you will see a Welcome page with a button “Get started with a playground”. You could also use the menu File -> New -> Playground…

Select iOS and Blank for the template and save it to your Desktop folder or somewhere you’ll remember.

Variables

Variables are the fundamental building blocks used to hold some bit of data. Let’s see what they are.

When you created a playground you would see this line.

var str = "Hello, playground"

This is a statement that declares a var (variable) named str that is of type String and sets its value to “Hello, playground”. Let’s break this down further.

The first word, var, is a keyword in swift which tells the compiler that we want to declare a variable. When declaring a variable you must also give it a name.

The second part, str, is the name of the variable. You can use almost any characters, including Unicode, in the name. However, the name can’t have any spaces or start with a number or contain any mathematical symbols. In general, you should choose a name that’s descriptive and meaningful. For names with multiple words, the convention is to use lower camel case format, which means that the first word is lowercase and each word following it begins with a capital letter. As an example, we can write the line as follows.

var greetingText = "Hello, playground"

After the name is the assignment operator (equals sign). This means that we take the value of the expression on the right-hand side and assign it to the variable. Later we can recall the value by referencing the name of the variable.

The final part of this is the value, “Hello, playground”. We can change this value however we like. Try changing it to your name.

There is one more piece of information needed when declaring a variable and that is the type. In this example, the type is defined, implicitly, as a String. But we can also declare is explicitly like so.

var greetingText: String = "Hello, iOS"

There are several built-in types and you can even create your own compound types. Here are a few simple examples.

var age: Int = 36
var isLearning: Bool = true
var progress: Double = 0.01

Although a variable can change its value, it cannot change its type. For example, we can later change the age variable to have the value 37 but we can’t set it to “hello”.

Constants

Similarly, we can also declare a constant using the keyword, let. The difference between constants and variables is that the value of a constant cannot be changed after it’s set, while a variable can change values as often as desired.

var age: Int = 36
age = 37

let birthYear = 1983
birthYear = 1982

When you type this example into your playground you will notice that the last line gives an error. That is because we have declared birthYear as a constant (let).

You can code your entire application without ever using constants, but that can lead to problems. Some things, like birthYear, do not change and should be constant to avoid accidental changes. Declaring a constant may also allow the compiler to better optimize your code giving better performance.

It may be difficult at first to know if you need a variable or a constant. As a rule of thumb, for people starting out, always start with a constant. You can update the declaration at a later point, if and when you need to update the value.

Next steps

So far you have learned how to declare and define variables and constants and the difference between them. You have also begun to learn about types and seen examples of the basic built-in types.

The next article will be about collections in Swift. Collections are types of variables that hold multiple values. Hope to see you there.


How to customize UISegmentedControl without losing your mind

March 25, 2018

Playground, UIKit

Comments Off on How to customize UISegmentedControl without losing your mind


The UISegmentedControl provided by UIKit is a ubiquitous control. However, when it comes to customizing this control, it can get pretty tricky. So I’m going to try to explain how this widget can be styled to better match your app by walking through a few different customizations.

The image below shows the different styles we will build.

Read More »


Master map, compactMap, flatMap, reduce and filter by creating your own implementation

February 21, 2018

General Coding, Playground, Reference

Comments Off on Master map, compactMap, flatMap, reduce and filter by creating your own implementation


There are a few higher-order functions for collections that can be rather difficult to fully understand. While there are several good resources out there to learn the basics, it can still be confusing and seem a bit like magic. And when things seem like magic they tend to not get used effectively or maybe not at all.

In this post, I’m going to try explaining map, compactMap, flatMap, reduce and filter by walking through their inputs, outputs and deriving an implementation for each. For simplicity, we will focus on arrays but this can be expanded to most data structures. The goal is only learning the ideas, not to implement the best possible solution.Read More »


Quick and dirty spotlight silhouette

November 25, 2017

Lighting, Playground, SpriteKit

Comments Off on Quick and dirty spotlight silhouette


This is a very simple spotlight effect that can be used to show silhouettes. No shaders or blend modes, just a couple sprites. The way this effect works is that you make the foreground and background elements the same color and then simply add an element with contrasting color between them.

Read More »


Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.





Swift Tweets