Building URLs in Swift

Can we be safe? GET AWAY STRINGS!

Steven Curtis
The Startup

--

Photo by Kon Karampelas on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

You might be using RESTful services, and using both GET and POST requests (and indeed more).

How can we easily access our API and the API Endpoints, while maximising reuse in our application?

The answer is contained below!

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Know something about commenting in Swift (guide HERE)

Terminology

API: Application programming interface. A set of accessible tools for building software applications

Endpoint: An endpoint is a remote computing device that communicates back and forth with a network to which it is connected

String: A collection of Characters

URL: Uniform Resource Locator. An address of a web page or resource.

Motivation

Simply creating a URL from a String is easy. Some Might Say that it is too easy in Swift and doesn’t need a second thought from developers.

Let us take the following URL as a simple sampleAPI endpoint…

--

--