Thursday, February 11, 2016

Hello World! Build Your First App Using Swift

By now you should have installed Xcode 7 and some understandings of Swift language. If you haven't done so, check out the previous chapter about what you need to begin iOS programming. We'll use Xcode 7.0 (or up) to work on all exercises in this book. You may have heard of the "Hello World" program if you have read any programming book before. Hello World is a program for the first-time programmer to create. It's a very simple program that outputs "Hello, World" on the screen of a device.


It's a tradition in the programming world. So, let's follow the programming tradition and create a "Hello World" app using Xcode. Despite its simplicity, the "Hello World" program serves a few purposes:

  • It gives you an overview of the syntax and structure of Swift, the new programming language of iOS.
  • It also gives you a basic introduction to the Xcode 7 environment. You'll learn how to create an Xcode project and lay out your user interface using Interface Builder. Even if you've used Xcode before, you'll learn what's new in the latest version of Xcode.
  • You'll learn how to compile a program, build the app and test it using the built-in simulator.
  • Lastly, it makes you think programming is not difficult. I don't want to scare you away from learning programming. It'll be fun. 

Your First App
Your first app, as displayed in figure 3-1, is very simple and just shows a "Hello World" button. When user taps the button, the app shows a welcome message. That's it. Extremely simple but it helps you kick off your iOS programming journey.

Let's Jump Right Into Create a Project
First, launch Xcode. Once launched, Xcode displays a welcome dialog. From here, choose
"Create a new Xcode project" to start a new project. Xcode shows various project templates for selection. For your first app, choose Application (under iOS) > "Single View Application" and click "Next".
 
You can simply fill in the options as follows:
  • Product Name: HelloWorld – This is the name of your app.
  • Organization Name: AppCoda – It's the name of your organization.
  • Organization Identifier: com.appcoda – It's actually the domain name written the other way round. If you have a domain, you can use your own domain * name. Otherwise, you may use "com.appcoda" or just fill in "edu.self".
  • Bundle Identifier: com.appcoda.HelloWorld - It's a unique identifier of your app, which is used during app submission. You do not need to fill in this option. Xcode automatically generates it for you.
  • Language: Swift – Xcode 7 supports both Objective-C and Swift for app development.
As this book is about Swift, we'll use Swift to develop the project.
  • Devices: iPhone – Select "iPhone" for this project.
  • Use Core Data: [unchecked] – Do not select this option. You do not need Core Data for this simple project. We'll explain Core Data in later chapters.
  • Include Unit Tests: [unchecked] – Do not select this option. You do not need unit tests for this simple project.
  • Include UI Tests: [unchecked] – Do not select this option. You do not need UI tests for this simple project. 

Familiarize Yourself with Xcode Workspace
Before we move on to the coding part, let's take a few minutes to have a quick look at the Xcode workspace environment. In the left pane is the project navigator. You can find all your project files in this area. 

The center part of the workspace is the editor area. You do all the editing stuff here (such as editing the project setting, source code file, user interface) in this area. Depending on the type of file, Xcode shows you different interfaces in the editor area. For instance, if you select ViewController.swift in the project navigator, Xcode displays the source code in the center area (see figure 3-7). If you select Main.storyboard, which is the file for storing user interface, Xcode shows you the visual editor for storyboard (see figure 3-8).



Run Your App for the First Time
Until now, we have written zero lines of code. Even so, you can run your app using the built-in simulator. This will give you an idea how to build and test your app in Xcode. In the toolbar you should see the Run button. If you hit the Run button, Xcode automatically builds the app and runs it in the selected simulator.

By default, the Simulator is set to iPhone 6. If you click the iPhone 6 button, you'll see a list of available simulators such as iPhone 4s and iPhone 6 Plus. As we're going to build an iPhone app, we simply use iPhone 6 as the Simulator. 

No comments:

Post a Comment