English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Swift is an open-source programming language used to develop OS X and iOS applications.
Before formal application development, we need to set up the Swift development environment to better use various development tools and languages for rapid application development. Since the Swift development environment needs to run on the OS X system, the setup of the environment will be different from the Windows environment. Let's learn how to set up the Swift development environment below.
The premise of successfully setting up the Swift development environment:
You must have a Mac computer. Because the integrated development environment XCode can only run on OS X system.
The computer system must be running OS 10.9.3and above.
The computer must install the Xcode integrated development environment.
Official website address of Swift development tools:https://developer.apple.com/xcode/download/.
Swift Source Code Download:https://swift.org/download/#latest-development-snapshots
After the download is complete, double-click the downloaded dmg file to install it. After the installation is complete, we will move the Xcode icon to the application folder.
You can also search for xcode installation in the App Store as shown in the figure below:
After the Xcode is installed, we can start writing Swift code.
Next, open Xcode in the application folder, select File => New => Playground at the top of the screen after opening.
Next, set a name for the playground and select the iOS platform.
Swift's playground is like an interactive document, which is used to practice learning Swift, write a line of code and get a line of result (on the right), and you can view the result of the code in real time, which is a powerful tool for learning the Swift language!
The following is the default code in 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 The code is as follows:
import Cocoa var str = "Hello, playground"
After the above program is loaded, the execution result of the program will be displayed on the right side of the Playground window:
Hello, playground
So far, you have completed the study of the first Swift program, congrats on your entry!
1Open the xcode tool, select File => New => Project
2Select 'Single View Application' and click 'next' to create a simple example app application.
3Enter the project name (ProductName), company name (Organization Name), and company identifier prefix (Organization identifier) next. You also need to select the development language (Language), and select the device (Devices).
Among them, there are two options for Language: Objective-c and swift, of course, we choose the swift option because we are learning swift. Click 'Next' to proceed to the next step.
4Select the directory where it is stored. If you want to use Git source code management, check 'Create git repository on My Mac' under Source Control. Click 'create' to create a project.
5After the project is created, a sample file is generated by default, which shows that swift has merged the h and m files in oc into one file (that is, a file with a swift suffix). Main.storyboard is equivalent to an xib file and has more features than xib.
6.Open main.storyboard, and you will see a simple blank application interface by default, with the size of a tablet interface. If you only need to develop apps compatible with iPhone, you can uncheck the Use Auto Layout option (it is checked by default).
7.A dialog box popped up, allowing us to select the interface size, iPhone or iPad. We select the iPhone size.
8.As you can see, the interface size has changed to the width and height of the iPhone.
You can remember the size of the interface for future layout calculations:
9.We add some content to the interface, find the Text control at the bottom right, drag it into the storyboard, and double-click to enter the text "Hello World!".
Run the simulator (command+Press the R shortcut or select Product => Run from the menu bar).
And that's it, our first Swift project is complete.