Hello world

Creating Hello World Using Swift

Creating Hello World Using Swift – is the first tutorial of our course. For building this tutorials we have used

  • Xcode 6
  • iOS 8
  • swift programming language

First let’s take a look at final app that we are about to build.

HelloWorld_Swift1

 

This will be a very simple app having one button “Say Hello”, when tapped ‘ll show the message. Thats it, no space science needed. It will also help you in getting familiar with the developing environment.

 

We assume that you already have Xcode. Just click on Xcode and you will see the following options on welcome screen.

HelloWorld_Swift2

Here we will choose “Create a new Xcode project”, we will also look at other option on this screen in later tutorials. Click on “Create a new Xcode project” and Xcode will display various project template.

HelloWorld_Swift3

 

Here We will choose “Single View Application” and press “Next”, you will be navigated to this screen:

HelloWorld_Swift4

 

Fill in the following options and simply proceed.

  • Product Name : HelloWorld – This will be the name of our app.
  • Organization Name : AppTuitions – you can use your own name or just as of now use mine.
  • Organization Identifier : com.apptuitions – This is used to create unique app identifier for your app. Its just the domain name written in the other way around.
  • Bundle Identifier : com.apptuitions.SimpleTable – As i said above this is the unique identifier for your app.
  • Language : Swift – We will be using swift as programming language for this tutorial.
  • Device : iPhone – Choose iPhone as off now. We will look into other options in later tutorials.
  • Use Core Data : Unchecked – Keep it Uncheck as of now we don’t need it. We will also  have a separate tutorial on this later on.

Tap on “Next” and Xcode will ask for the location where you want to save your app.

HelloWorld_Swift5

 

Now select a folder or create new and press “Create”. You will get the following screen.

HelloWorld_Swift6

 

Now, Select the “Main.storyboard” .

HelloWorld_Swift7

 

  1. Select Main.storyboard
  2.  Select view
  3. Select “iPhone 4 inch” from dropdown menu for size(We will explain this later on why we are doing this.)
  4. select “Portrait” from the dropdown menu for orientation.

The app is ready. Go press “Run” button on the top left corner of Xcode.

HelloWorld_Swift8

 

and you will get it running on simulator

HelloWorld_Swift9

 

Its blank, isn’t it? Don’t worry we just need little code snippet to make is say “Hello World”. :) Press “Stop” button to  terminate the app.

 

HelloWorld_Swift10

 

Now from the project navigator select the Main.storyboard .you will see the following empty view.

HelloWorld_Swift11

 

In the lower portion of utility area, you will find different view components. from here we have to drag and drop the “Button” element into our empty view as described in below image.

HelloWorld_Swift12

 

Now, rename button’s title by double clicking on it. Here we will name it as “Say Hello”, you can name it whatever you like.

HelloWorld_Swift13

 

Lets Add Code To This Button

Now, Press “Show the assistant editor” button in the upper right corner of utility area.

HelloWorld_Swift14

 

And you on following screen.

HelloWorld_Swift15

 

Adding An Action To The Button

 Ok, going good so far. Now, press and hold Control key and select the Button and drag and drop into ViewController.swift class. The screen should look like this.

HelloWorld_Swift16

Now release the mouse click after drag and drop and set connection to “Action” and add method name as “buttonPressed”.

HelloWorld_Swift17

 and press “connect”.

 Now, you can see the function “buttonPressed” which will be triggered when user touches the button.

We are almost there

 Now add the following code to your “buttonPressed” function.

1
2
3
4
5
var alertView = UIAlertView()
alertView.addButtonWithTitle("Ok")
alertView.title = "My first swift app!!"
alertView.message = "Hello World"
alertView.show()

 and your ViewController.swift class and “buttonPressed” function will look like this.

HelloWorld_Swift18

THATS IT!! Go run your app and we are done with our first iOS Swift tutorial.

 Thanks for visiting. More tutorials will be added soon. Stay Tuned.