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:
But thats not all. In this tutorial, we will take you through the different steps for creating your first Swift application using Xcode.
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.
In the welcome window, click “Create a new Xcode project” as shown below
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
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
- 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.
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.
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
Place the button centre of the Scene and add two following constraints “Horizontal in container” and “Vertical in container”
Once done with adding constraints, click on Show assistant editor
Now Create Button Action by control + Drag from Button to the ViewController.Swift
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