Home » How - To / Tutorial » Programming » How to Create iOS Swift “Hello World” Program

How to Create iOS Swift “Hello World” Program

Swift is the new programming language introduced by Apple to slowly replace Objective- C. It offers advanced features which are not available in the Objective-C programming language. In Swift, programmers can combine Objective-C++, Objective-C, and Swift in just a single program so as to take advantage of the features from the three languages, meaning that one can create cool and amazing apps with this language.

Hello World App

Before creating our Hello World app, download the beta version of Xcode 6. You can get this from the iOS developer center. Once you get through, create a new project by clicking on:

File->New->New Project

A widow will appear with some options for you to choose. Select “Single view Application.”

New-Project-iOS-Swift How to Create iOS Swift "Hello World" Program

Give the project a name of choice. Select the language followed by choosing Swift. Choose a directory to store your project. It will be over and ready to go. Note that in there Swift programming language, there are no headers and whatever is coded goes to the .Swift file.

Two template files are also created, that is, ViewController.swift and AppController.swift. If you run the app at this time, you get an empty view, so wait until we develop some UI controls. Notice that after you open the Storyboard, the view controller will have a different size, neither that of the iPad nor the iPhone.

Start by dragging a label, followed by text field, and finally a button. Align them to make sure that they are in the position that you want.

Layout-iOS-Swift-Programming How to Create iOS Swift "Hello World" Program

It is now time to create the outlets. Just start by selecting the Label, hold down the Ctrl key, and then drag it to the file on the right window. This will form the ViewController.swift file. Repeat this for three controls so that you can have three outlets.

Simple-Hello-World-iOS-Swift-Programming How to Create iOS Swift "Hello World" Program


The three outlets will be as follows:

@IBOutlet var helloLabel : UILabel

@IBOutlet var nameTextField : UITextField

@IBOutlet var helloButton : UIButton

If you are good at programming with Objective-C, you will notice some similarities with Swift, such the IBOutlet keyword. However, there is a new keyword “override.” This keyword will be used in case you override a method in the super class. Swift has brought about the prevention of common mistakes that programmers make since with this keyword, overriding methods without realizing it will not be possible.

We then need to declare a method to help in setting up the user interface. This method will be as follows:

Notice the syntax of declaring a method in Swift. The method has no arguments and it returns nothing. Our aim is to display a red text at the center of the label written “Hello World.” The code below will achieve this:

Notice that we have not used semicolons at the end of the lines, nor the symbol @ before string literals. Methods for specifying color have also been specified rather than sending some message to UIcolor. For the purpose of guiding the user, let us add a placeholder to the text field:

To change the text displayed on the button:

Now we are done with creating the user interface. It’s time to call the method that we created earlier on. This can be done as follows:

We can now run our app. However, let us add some functionality to the button. To do this, just bring up the assistant editor while in Interface Builder. Drag to the view controller from the UIButton by pressing the Ctrl key. Name the method as helloAction. This should be done after moving to action from outlet.

iOS-Swift-Programming-outlet How to Create iOS Swift "Hello World" Program


We want to make the label display the text entered in the text field. If nothing was entered in the text field, an alert dialog should be shown prompting the user to enter some text. This can be achieved using the code below:

First, we got the text from the text view and stored it in a constant variable. Constant variables are highly useful in Swift. We then check whether the constant is empty or contains some text. If it is empty, we provide an alert box prompting the user to enter his name.

If it has some text, this name is combined with the word “Hello,” so the output will be “Hello name,” where the name is the text that you entered in the text field. This has really shed some light to you as far as Swift is concerned.

If you have a little programming experience, the language will be easy for you. Notice that we have used UIAlertController instead of the UIAlertView. This is because in iOS 8, the latter can easily crash our app, so we are trying to avoid this.

You can now run the app on an iPhone simulator. Use iPhone 5s. The following will be the output:

Finished-Hello-World-iOS-App How to Create iOS Swift "Hello World" Program


When you enter your name on the text field and press the enter key, you will observe a change in the red text, since the “Swift” will change to the name that you have entered. That is your first app, which is very amazing.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *