Swift is an open-source programming language used for developing OS X and iOS applications.
Before officially developing applications, we need to set up the Swift development environment to better utilize various development tools and the language for rapid application development. Since the Swift development environment needs to run on the OS X system, its setup will be different from the Windows environment. Let's learn how to set up the Swift development environment together.
Prerequisites for successfully setting up the Swift development environment:
1. You must own an Apple computer. Because the integrated development environment Xcode can only run on the OS X system.
2. The computer system must be OS 10.9.3 or above.
3. The computer must have the Xcode integrated development environment installed.
### Swift Development Tool Xcode Download
**Swift development tool official website address:**[https://developer.apple.com/xcode/download/](https://developer.apple.com/xcode/download/).
**Swift source code download:**[https://swift.org/download/#latest-development-snapshots](https://swift.org/download/#latest-development-snapshots)
After the download is complete, double-click the downloaded dmg file to install it. After installation, we will move the Xcode icon to the Applications folder.
!(#)
You can also search for xcode in the App Store to install it, as shown in the figure below:
* * *
## First Swift Program
After Xcode is installed, we can start writing Swift code.
Next, we open Xcode in the Applications folder. After opening, select File => New => Playground from the top of the screen.
!(#)
Then set a name for the playground and select the iOS platform.
!(#)
Swift's playground is like an interactive document. It is used for practicing and learning Swift. You write a line of code and get a line of results (on the right side). You can view the code results in real-time. It is a powerful tool for learning the Swift language! !(#)
The following is the default code in the Swift Playground window:
import UIKitvar str = "Hello, playground"
If you want to create an OS X program, you need to import the Cocoa package **import Cocoa**. The code is as follows:
import Cocoavar str = "Hello, playground"
After the above program is loaded, the execution result will be displayed on the right side of the Playground window:
Hello, playground
At this point, you have completed the learning of your first Swift program. Congratulations on getting started.
* * *
## Create the First Project
1. Open the xcode tool, select File => New => Project
!(#)
2. We select "Single View Application" and click "next" to create a simple example app application.
!(#)
3. Next, we enter the project name (ProductName), company name (Organization Name), company identifier prefix (Organization identifier), and also select the development language (Language) and select the device (Devices).
There are two options for Language: Objective-c and swift. Since we are learning swift, of course we choose the swift option. Click "Next" to proceed.
!(#)
4. Select the storage directory. If you want to use Git source code management, check the create git repository on My Mac under Source Control. Click create to create the project.
!(#)
5. After the project is created, an example file is generated by default. You can see that swift merges the h and m files in OC into one file (i.e., a file with the swift suffix). Main.storyboard is equivalent to the xib file, but with more features than xib.
!(#)
6. Open main.storyboard, and by default you will see a simple blank application interface, the size of a tablet interface. If developers only need to develop apps compatible with the iPhone, the checkmark for Use Auto Layout can be removed (it is checked by default).
!(#)
7. A dialog box pops up, letting us choose the interface size, iPhone or iPad. We choose the iPhone size.
!(#)
8. You can see that the interface size has changed to the width and height of the iPhone.
You can remember the interface-related sizes to facilitate layout and position calculation in the future:
!(#)
9. Let's add some content to the interface. Find the Text control in the lower right corner, drag it onto the storyboard, and double-click to write the text "Hello World!".
!(#)
Run the simulator (command+R shortcut key or select Product => Run in the menu bar).
!(#)
At this point, our first Swift project is complete.