Easy Tutorial
❮ Swift Optional Chaining Swift Break Statement ❯

Swift Environment Setup

Swift is an open-source programming language used for developing OS X and iOS applications.

Before starting to develop applications, we need to set up the Swift development environment to facilitate the use of various development tools and languages for rapid application development. Since the Swift development environment needs to run on the OS X system, its setup will differ from that on Windows. Let's learn how to set up the Swift development environment together.

Prerequisites for successfully setting up the Swift development environment:

Download Xcode for Swift Development

Swift Development Tool Official Website: https://developer.apple.com/xcode/download/

Swift Source Code Download: https://swift.org/download/#latest-development-snapshots

After downloading, double-click the downloaded dmg file to install. Once installed, move the Xcode icon to the Applications folder.

You can also search for Xcode in the App Store and install it, as shown below:


First Swift Program

After installing Xcode, we can start writing Swift code.

Next, open Xcode from the Applications folder, and at the top of the screen, select File => New => Playground.

Then, set a name for the playground and select the iOS platform.

Swift's playground is like an interactive document, used for practicing and learning Swift. Writing a line of code produces a result (on the right), allowing real-time viewing of code results, which is a great tool for learning the Swift language!

Here is the default code for the Swift Playground window:

import UIKit

var str = "Hello, playground"

If you want to create an OS X program, you need to import the Cocoa package with import Cocoa. The code is as follows:

import Cocoa

var str = "Hello, playground"

After loading the above program, the execution result will be displayed on the right side of the Playground window:

Hello, playground

You have now completed your first Swift program. Congratulations on getting started.


Creating the First Project

  1. Open Xcode and select File => New => Project.

  2. Choose a "Single View Application" and click "Next" to create a simple example app.

  3. Then, enter the project name (ProductName), company name (Organization Name), company identifier prefix (Organization identifier), and select the development language (Language) and devices (Devices).

    There are two options for Language: Objective-C and Swift. Since we are learning Swift, we choose Swift. Click "Next".

  4. Select the directory to store the project. If you want to use Git for source code management, check "Create git repository on My Mac" under Source Control. Click "Create" to create the project.

  5. After the project is created, a sample file is generated by default. You can see that Swift combines the h and m files of Objective-C into one file (the Swift file extension). Main.storyboard is equivalent to the xib file but with more functionality.

  6. Open main.storyboard to see a simple blank application interface, which is the size of a tablet interface by default. If you only need to develop an app compatible with iPhone, you can uncheck "Use Auto Layout" (checked by default).

  7. A dialog box pops up, asking us to choose the interface size: iPhone or iPad. We choose the iPhone size.

  8. The interface size changes to the width and height of an iPhone.

    Remember the relevant dimensions of the interface for future layout calculations:

  9. Add some content to the interface. Find the Text control in the lower right corner, drag it onto the storyboard, and double-click to enter the text "Hello World!".

Run the simulator (shortcut Command+R or select Product => Run from the menu bar).

You have now completed your first Swift project.

❮ Swift Optional Chaining Swift Break Statement ❯