While learning a new technology or framework, it is a traditional practice for any developer to start with “Hello World” program. Since this is our first step towards learning Swift programming, let us get started with the same.

In Swift we can print “Hello, World” by simply writing the following snippet:

Print("Hello, World")

But thats not all. In this tutorial, we will take you through the different steps for creating your first Swift application using Xcode.

Prerequisites

Before we begin, we need the following stuffs in place:

  • Mac system running on Mac OS X version 10 or higher.
  • Xcode installed and running on the machine. If it is not installed, download it free from App Store.
  • Basic knowledge on Object orient programming like Objective-C and Swift is a bonus.  Though it is not mandatory.

What is Xcode IDE?

Xcode is it’s an powerful IDE (integrated development environment) provided by Apple. Xcode provides everything you need to kick start with app development. Following are some of the Xcode features.

  • Using Xcode we can build apps for Mac, iOS, and Watch OS.
  • Xcode IDE is bundled with a rich code editor, debugger, search tool, code refactoring, language syntax highlighting, intelligent code auto complete and many more.
  • Xcode supports Swift, Objective-C, C and C++, and other languages.
  • Xcode Storyboard and UI builder brings a drag-and-drop interface using which developers can rapidly build UI for different resolution devices.

There are many more features that Xcode supports which are intended for faster development and make developers productive. As you go along you will discover something new.

Crating New Xcode Project

Time to get your hands dirty by launching Xcode Straight away, where you land up in the following screen short.

Screen Shot 2016-08-14 at 5.00.16 PM

In the welcome window, click “Create a new Xcode project” as shown below

Screen Shot 2016-08-14 at 5.00.16 PM

Now Xcode opens a new window and displays a dialog in which you choose a template following these steps iOS > Application > Single View Application > next

For Selecting the Application Templets

For Selecting the Application Templets

In the dialog that appears, use the following values to name your app and choose additional options for your project:

  • Product Name: The name of your application. Lets keep it HelloWorld. Xcode uses the product name you entered to name your project and the app.
  • Organization Name: The name of your organization or your own name. This is optional, hence you may leave this blank.
  • Organization Identifier: It is usually the reverse of your primary business domain. For example, if your business domain is stacktips.com then the organization identifier can be com.stacktips.
  • Bundle Identifier: This value is automatically generated based on your product name and organization identifier.
  • Select Swift from the language drop down.
  • Devices: Select Universal. A Universal app is one that runs on both iPhone and iPad.
  • Use Core Data: Unselected.
  • Include Unit Tests: Unselected.
  • Include UI Tests: Unselected.
  • Click Next.
Choose options for your new project

Choose options for your new project

In the dialog that appears, select a location to save your project and click Create button.  Once your project is created, Xcode opens your new project in the workspace window.

Xcode opens your new project in the workspace window

Xcode opens your new project in the workspace window

Open Main.storyboard file, drag a button from the object library and drop it on ViewController Scene , change the name of the button by double tapping the button or change in Attributes Inspector Title value as shown below

DragDrop Button form Object Library to Scene

DragDrop Button form Object Library to Scene

Place the button centre of the Scene and add two following constraints “Horizontal in container” and “Vertical in container”

Adding Constraints

Adding Constraints

Once done with adding constraints, click on Show assistant editor

Show Assistant Editor

Show Assistant Editor

Now Create Button Action by control + Drag from Button to the ViewController.Swift

Button Action-Target

Button Action-Target

Later open ViewController.swift form Project Navigator of the XCode, and place the following code inside the Action-Target method created:

let alert = UIAlertController(title: "StackTips Alert", message: "Hello, World",   preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

Run the application, Click on HelloWorld Button. Bang, you will get the alert saying “Hello, World” you can find the source code in the following

Download

Happy Coding…!

This article is about: Swift,

Satish Kumar

I am a self-driven software professional from Bangalore who live with a desire to set new examples, something that can create a unique signature of my own. I am a very friendly, fun loving person. I love to interact with people and make new friends who can bring difference in my life. I strive to become a better programmer.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.