A Hello App In Swift

It is traditional to begin learning a new programming language by writing a “Hello, World” program. Since Swift is Apple’s new programming language for apps, I thought it only proper to write “Hello, Swift.” So here we go! (The source code for this project is here:¬†HelloSwift)

If you haven’t already downloaded and installed Xcode 6 with iOS 8, now would be a good time to do that. You can get it at developer.apple.com.

OK, so you’ve got Xcode loaded up and ready to go, and you’ve started a new project for iPhone (at least I assume that’s what you’ve done; if not, go ahead and do that now). Make sure that the language setting for the project is Swift (not Objective C). Now open up Interface Builder by selecting the Main.storyboard file from the list on the left.

The first thing you will probably notice is that the layout looks nothing like any device Apple has ever produced. Apple is telling us that the screen size in Interface Builder is an abstract concept; we can build apps that will run on different physical screen sizes using a set of layout constraints defined in the storyboard. We’ll talk about these constraints in detail in future blog posts. For now, click on the control at the bottom of the screen that reads “Any Any” and manipulate the blue box so that the constraints for the layout read “Compact Any.”

blogSep714_1

 

 

 

 

 

 

 

 

 

 

Now our layout looks a bit better. Next, let’s drag a label and a button to the layout, move them about on the view so they look something like this:

blogSep714_3

 

 

 

 

 

 

 

 

 

 

It looks good, but if you were to run the app now, you would find that the controls are not centered in the simulator! The next step is to align the label and the button horizontally in the view. With the label selected, click on the “Align” constraint button at the bottom of the interface. Choose “Horizontal Center in Container” and click the “Add 1 Constraint” button. Do the same for the button.

blogSep714_2

 

 

 

 

 

 

 

 

 

 

 

You will notice that the constraints are shown in orange. This is because there are missing constraints. In this case, we’ve specified horizontal constraints, but no vertical constraints. Let’s fix that now.

With the label selected, click the “Pin” constraint button.

blogSep714_4

 

 

 

 

 

 

 

 

 

 

 

Click on the orange spacer between the picture of the control and the top constraint, as shown in the image above. Notice that it changes from a dotted to a solid line, indicating that the constraint will be used in positioning the control. Now click on the “Add 1 Constraint” button. Follow exactly the same steps for the button.

With our constraints set up, we can now turn to the code. Open ViewController.swift. In Swift, there are no separate interface and implementation files; the entire class definition is located in a single .swift file. We’ll add a property for the label and an action method for the button like so:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var label:UILabel?

    @IBAction func buttonPress (sender:UIButton) {
        label?.text = "Hello, Swift"
    }

}

If you’re familiar with Objective C, the code above will look somewhat familiar, but also somewhat strange. The property for the label is declared as an @IBOutlet, meaning it’s an outlet for Interface Builder, as a var, meaning that it’s a variable,¬†given the name “label,” and declared to be of type UILabel?, meaning its value will either be a UILabel or nil. Declaring properties as optional types is a good habit to get into with Swift, it will save many headaches later.

The buttonPress method is declared to be an @IBAction, meaning that it will be wired up as a callback in Interface Builder, as a func, meaning that it is of type function (in Swift, all methods are functions), given the name “buttonPress,” and the parameter list is a UIButton control named sender. In the method, we set the label’s text to be “Hello, Swift.”

All that remains is to wire up the button and label, which we do in the same way as in Xcode 5:

blogSep714_5

 

 

 

 

 

 

 

 

 

 

 

 

 

…and now we’re ready to run! When the app is running in the simulator, click the button, and watch the text in the label change to “Hello, Swift.” Alright, there’s not really much going on here, but now you know how to start building an iPhone app in Xcode 6 with iOS 8.

Have fun!

Leave a Comment: