Swift environment building


Release date:2023-11-23 Update date:2023-12-11 Editor:admin View counts:189

Label:

Swift environment building

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

Before the formal development of applications, we need to build a Swift development environment, in order to better use a variety of development tools and languages for rapid application development. Because the Swift development environment needs to run in the OS X system, so the constructionof its environment will be different from the Windows environment. Let’s learn how to build the swift development environment.

The prerequisites for 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 or above.

  3. The computer must have the Xcode integrated development environment installed.

Download Swift development tools Xcode

The official website address of Swift development tools is 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, and after the installation is complete, we will move the Xcode iconto the application folder.

Image0

You can also search for xcode installation in App Store, as shown in the following figure:

The first Swift program

After the Xcode installation is complete, we can start writing Swift code.

Next, we open Xcode in the application folder, and select File = > New = > Playground at the top of the screen.

Image1

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

Image2

Swift’s playground is like an interactive document, it is used to practice hand-learning swift, write a sentence of code to produce a line of results (right), you can view the code results in real time, is a sharp tool to learn the swift language!

Image3

The following 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``import Cocoa`` the code is as follows:

import Cocoa

var str = "Hello, playground"

After the above program is loaded, the program execution result 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. Congratulations on getting started.

Create the first project

1.Open the xcode tool and select File = > New = > Project

Image4

2.Let’s select a “Single View Application” and click “next” to create asimple example app application.

Image5

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

Language has two options: Objective-c and swift, because we are learning swift, of course, choose the swift item. Click “Next” next.

Image6

4.Select the directory to store. If you want to use Git source control, youwill check Source Control’s create git repository on My Mac. Click create to create the project.

Image7

5.After the project is created, a sample file is generated by default. You can see that swift merges the h and m files in oc into a file (that is, the swift suffix file). Main.storyboard is the equivalent of a xib file and has more functions than xib.

Image8

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

Image9

7.A dialog box pops up, let’s choose the interface size, iPhone or iPad. We choose the size of the iPhone.

Image10

8.You can see that the interface size has changed to the width and height of the phone’s iphone.

You can remember the dimensions related to the interface to facilitate the layout and calculation location in the future:

Image11

9.We add something to the interface, find the Text control at the bottom right, drag it onto the storyboard, and double-click to write the text “Hello World!”.

Image12

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

At this point, our first Swift project is complete.

Powered by TorCMS (https://github.com/bukun/TorCMS).