Beginner Series: Swift Tutorial
Please keep in mind that all editing for Swift is done in Xcode, Apple’s editing tool.
Create a New Project
To begin, open Xcode, and do the following:
Click File > New > Project
From the template menu, choose the Single View Application option. Click Next and follow the prompts to enter the following details:
- Product Name: Enter the name of your application.
- Organization Name: This would be the name of your development studio.
- Organization Identifier: Formatted as a reverse domain name, so com.apple.
- Language: Swift
- Devices: iPhone
- Use Core Data: Leave unchecked.
- Click the Next Button to save the project. Make sure you choose a directory you’ll recognize later.
Xcode will open, and you will be presented with the full development interface. Don’t be alarmed, in time, you’ll grow to know almost all of the options available.
Edit the View Controller
For now, use the Navigator pane on the left to select the Main.storyboard file. This will open to show the controller scene view. For all intents and purposes, this is a simulation of the phone display where the application will be output. A “view controller” is nothing more than a screen.
You may notice right away that the view controller is square, whereas iPhone displays are definitely rectangular. This is because the development space is confined so that the elements will conform to multiple display sizes since there are a couple different size iPhones.
You can turn this feature off to have a rectangular space to work with, but for today, we’re not going to do that.
Next, we’re going to add a button to our interface, which is something new for “hello world” tutorials.
Select the Object Library. The list of objects that you see are pretty self-explanatory. These are interactive system objects – like buttons – that you can add to your interface.
Choose the following option:
Button (blue font) – Intercepts touch events and sends an action message to a target object when it’s tapped.
To add the button to your interface, click on the object and drag it onto the view controller. Ignore any blue lines you see for now, and place the button anywhere you like.
Double-click over the top of the button and change the text to “This is a button.” Feel free to change it to a custom message if you like.
Write Swift Code
Next, we’re actually going to write some code using Swift. First, we must open the selected view controller in the Assistant Editor.
To do this, open the Assistant Editor using the button in the top right corner of the window. This will open a file with some preexisting code. This file is called ViewController.swift and it is for editing the code related to the view controller we currently see on the screen.
Scroll down to the viewDidLoad function, which should look like this:
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. }
This function is designed to execute as soon as the view controller loads on the device. This is meant for applications that only have one view, which ours does. In addition, we want our message to appear right away so we will add our “hello world” code to the bottom of this function.
Below it, write the following:
print("Hello world")
Again, you can change the custom message to anything you want, but be sure to leave the rest of the tags alone.
Take note of that fact that both of these code snippets are devoid of semi-colons. This is because you do not need to write them when working with Swift. You can, if you would like to, however, it is not necessary.
Sending Hello World
To see the text, you must now run the application on a simulated device.
To do this, find the SP Test menu option (play button) and select it. If you want to test your application on a specific device, choose the schemes menu option next to the button and you will be presented with a drop-down menu of all the available Apple devices from the iPad 2 to the iPhone 6.
Choose the device you want and then click play. This will run the application simulator in Xcode.
*Note: If this is your first time running the application simulator on your Mac, you will be prompted to allow Developer Mode to be enabled. Also, be patient as the application takes time to build. It can take a few minutes and you may be tempted to kill the process, DON’T! The Apple logo will appear with a load bar while loading the app, just like when your Apple device starts.
As soon as the application loads, you will see a pane appear at the bottom of the window. This is called the Debug pane and is used to view content, and identify errors, among other things.
On the right-hand side of the debug pane, you should be able to see the text we added to the view controller.
In our case, we would see “hello world,” but your message may be different.
Now, to see our Button, take a look at the Simulator and you should see it right where you placed it on the Main.storyboard!
If it worked, congratulations! You just wrote your first iPhone application using Swift!
Next, we’ll take a look at some more Swift resources to help better learn Swift.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.