Developing a Simple Swift Application

We’re going to get a brief taste of Swift by building a very, very simple application for iOS.

This simple application is extremely cutting-edge: it will display a single button that, when tapped, will pop up an alert and change the button’s label to “Test!” We’re going to build on the project we created earlier in Chapter 1, so make sure you have that project open.

It’s generally good practice to create the interface first and then add code. This means that your code is written with an understanding of how it maps to what the user sees.

To that end, we’ll start by designing the interface for the application.

Designing the Interface

When building an application’s interface using Cocoa and Cocoa Touch, you have two options. You can either design your application’s screens in a storyboard, which shows how all the screens link together, or you can design each screen in isolation. As a general rule, storyboards are a better way to create your interfaces even if you only have a single view, as in the case of this first application we are building. The reason is that if you later want to give your application more than one view, it will be easier to do that in a storyboard.

Start by opening the interface file and adding a button. These are the steps you’ll need to follow:

  1. First, we’ll need to open the main storyboard. Because newly created projects use storyboards by default, your app’s interface is stored in the Main.storyboard file.

    Open it ...

Get Developing a simple Swift application now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.