Swift Environment Build

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

Before we can formally develop applications, we need to build a Swift development environment to be more user-friendly and use a variety of development tools and languages for rapid application development. S ince the Swift development environment needs to run on the OS X system, the environment will be built differently from the Windows environment, so let's take a look at how the Swift development environment is built.

The forefist of successfully building a swift development environment:

  1. You must have an Apple computer. Because the integrated development environment XCode can only run on OS X systems.
  2. The computer system must be OS 10.9.3 and above.
  3. The computer must be installed in the Xcode integrated development environment.

Swift development tool Xcode download

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

Swift development tool Baidu Software Center download (domestic faster): http://rj.baidu.com/soft/detail/40233.html

Swift source code download: https://swift.org/download/#latest-development-snapshots

Once the download is complete, double-click on the downloaded dmg file installation, and when the installation is complete we kick the Xcode icon to the app folder.

Swift Environment Build

You can also search for xcode installations in the App Store, as shown in the following image:


The first Swift program

Once the Xcode installation is complete, we're ready to start writing Swift code.

Next, we open Xcode in the App folder, and when we open it, select File, New, Playground at the top of the screen.

Swift Environment Build

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

Swift Environment Build

Swift's playground is like an interactive document, it's used to practice hand learning swift, write a line of code out of the results (right), you can see the code results in real time, is a tool to learn the swift language! Swift Environment Build

Here's 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 import Cocoa code as follows:

import Cocoa

var str = "Hello, playground"

When the above program is loaded, the results of the program execution are displayed on the right side of the Playground window:

Hello, playground

Now that you've completed your first Swift program, congratulations on getting started.


Create the first project

1. Open the xcode tool and select File

Swift Environment Build

2, we choose a "Single View Application" and click "next" to create a simple sample app.

Swift Environment Build

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

There are two options for Language: Objective-c and Swift, because we are learning swift of course to choose swift items. Click "Next" to go next.

Swift Environment Build

4, choose the stored directory, if you want to use Git source control, will be hooked on Source Control's creative git repository on My Mac. Click Create to create the project.

Swift Environment Build

5, after the project was created, the default generated a sample file, you can see swift oc in the h and m files merged into a file (i.e. swift suffix file). Main.storyboard is the equivalent of a xib file and has more features than xib.

Swift Environment Build

6, open the main.storyboard, by default to see a simple blank application interface, the size of the tablet interface. If you only need to develop apps that are compatible with your iPhone, you can tick off the Use Auto Layout (the default is tick).

Swift Environment Build

7, pop up a dialog box, let us choose the interface size, iPhone or all iPad. We choose the size of the iPhone.

Swift Environment Build

8, you can see that the size of the interface has changed to the width and height of the phone iPhone.

You can remember the dimensions related to the interface, so that you can calculate the location of the layout later:

The iPhone or iTouch is 320 pixels wide, 480 pixels high, the status bar is 20 pixels high, the toobar is 44 pixels high, the tabbar is 49 pixels high, and the navigation bar is 44 pixels high.

9. Let's add something to the interface, find the Text control at the bottom right, drag it into the storyboard, and double-click on the text "Hello World!".

Swift Environment Build

Run the emulator (the command-R shortcut or select Product sgt; Run) in the menu bar.

Swift Environment Build

At this point, our first Swift project is complete.