blog posts

Learn how to create Hello World program with Swift language

How to create hello world iOS app using xcode; In fact, we learn the first example of Hello world using swift; We will also give an example of how to use the emulator in iOS, how to create and run an iOS application using xcode.

Hello World iOS app in Swift language

The first and foremost program of all the programming languages ​​that you know well, the program Hello, world! Is. Now we will see how we can create the hello world app on iOS with Swift and using xcode.

Create the Hello, iOS world app in Xcode

To create a new project in Xcode on iOS, open Xcode from the Folder / Application list. After opening Xcode, the welcome window will open as shown below. In the Welcome window, click on the second option Create a new Xcode Project, which means “Create a new Xcode Project” or go to FlieNewProject.

C: \ Users \ mohammad \ Desktop \ open-xcode-to-create-new-ios-app-using-xcode.png

After selecting “Create a new Xcode project”, a new window will open in which we must select our template.

The new Xcode window includes several built-in application templates to implement the usual type of iOS apps. In this section we can choose what kind of application we want to program; For example, the type of program can be: page-based programs, tab-based programs, games, table-based programs, and so on. These templates have a preset interface and source code files.

For example, hello world! iOS, we will use the most basic “single demo” template. To select this item, go to the iOS section on the left to select the application, you must go to the Application path and in the main part of the window, select the “Single view Application” option and click on the next button, as shown below.

C: \ Users \ mohammad \ Desktop \ ios-swift-select-single-view-application-in-xcode.png

After clicking Next, we will see a window like the one below, in this case we have to mention the project name and other details related to our program.

Product Name: “Hello world!”

Everything we enter in the Product Name section is used for the project and application.

Organization Name Organization Name: “Tutlane”

You can enter the name of the organization or your name or you can leave it blank.

Organization Identifier: “com.developersocociation”

If you do not have an organizational ID; You can enter com.example.

Bundle Identifier

This value is automatically generated based on the values ​​we entered in the product sections and the organization ID.

Language: “Swift”

Select the language type “Swift”; Because we want to develop applications using swift.

Devices: “Universal”

Select the Universal Devices option, which means that the app runs on all iOS devices; If you want the app to run on iPad only, you can select the iPad option to restrict your app to running only on iPad devices.

Use Core Data: Unselected not selected

This option is used for database operations. Select this option if you are doing any database-related operations in your application, otherwise do not select this option.

Include Unit Test: Unselected not selected

If you need unit testing for your applications; Select this option otherwise do not select it.

Include UI Test (Includes UI tests): Not selected

Select this option if you need UI testing for your applications, otherwise do not select it.

After completing the options, click the Next button as shown below.

C: \ Users \ mohammad \ Desktop \ enter-name-to-create-new-project-in-ios-xcode.png

When you click the Next button, a new window will open where you want to save the program; Let’s choose. Once you have selected the project storage location; Then as shown in the figure below; Click the Create button.

C: \ Users \ mohammad \ Desktop \ save-xcode-new-project-in-application-folder-in-ios-swift.png

After clicking Create, Xcode opens a new project. By default, the structure of our project will be as follows.

C: \ Users \ mohammad \ Desktop \ ios-swift-xcode-default-project-structure (1) .png

Introduction to Xcode

Xcode provides everything, including interfaces for designing applications, editors for writing code, debuggers for debugging code, and various options for building and running our applications.

In the following; The main areas that exist in Xcode; We often use them to design, code, build and run our applications. You do not need to worry about these parts. We will learn in detail every part we use in our programs.

In our project, Main.storyboard and ViewController.swift are the main files that we used to design the application interface and maintain the source code.

Main.storyboard – is an intuitive interface editor and we will use this file to design the application interface

ViewController.swift – contains the source code of our program and we use this file to write the program code.

Now select the Main.storyboard file, Xcode opens the visual interface editor as shown below.

C: \ Users \ mohammad \ Desktop \ ios-swift-main-storyboard-file-structure-in-xcode (1) .png

Now select the ViewController.swift file in your project, the view of which is as shown below.

Viewcontroller.swift file in iOS Xcode project with example

Add iOS UI controls to the view

Now we need to add controls to our application for the library of available objects. The object library appears at the bottom right of Xcode. In case you did not find the object library; Click the button below, which is the third icon to the left of the library selector bar. (You can also navigate through the alternate ViewUtilitiesshow Object library path).

As mentioned, our interface will be in the Main.storyboard file; So open the Main.storyboard file. Now in the Objects Library, look for the Label in the box you see under Filter, then select the label and drag it as shown below to the Main.storyboard view controller; Release the label.

C: \ Users \ mohammad \ Desktop \ drag-label-control-in-ios-xcode-project.png

Now we will change the text of the label, for this purpose right click on the label to open the menu of label properties; In the text box, type the text property of the phrase Hello world. When we changed the label text; We change the position of the label control; To do this, go to the label control and click on label control and then move the label control as you can see below.

C: \ Users \ mohammad \ Desktop \ change-name-of-label-in-ios-swift-xcode-editor.png

Then click on Control label and moving it, can several features we see are: Center Horizontally in Container “horizontally in the center of the box”, Center Vertically in Container “vertically in the center of the box” . You can see these features in the figure below.

C: \ Users \ mohammad \ Desktop \ control-with-align-properties-in-ios-swift-xcode-editor.png

To control the label, we use the above features to place our label in the pivot position of the screen. After setting these two features, we can see two lines that intersect at a point like the image below. This way the label is placed at that point.

C: \ Users \ mohammad \ Desktop \ label-control-center-positioned-in-ios-swift-xcode-editor.pngWe are now running and reviewing the output of the Hello world program; For this we need a simulator.

Run the Xcode program

To build and run the program we use the Simulator in Xcode. The emulator helps us to know how the program will appear and behave if it runs on the device.

The emulator in Xcode has various options that we can use to run our applications on multiple devices such as iPad and iPhone with different screen sizes; Let’s try. Using these options, we can simulate our application on any device and test its design and behavior based on our needs.

C: \ Users \ mohammad \ Desktop \ simulator-in-xcode-to-run-application-ios-swift.png

Now based on our needs; Select your emulator from the Xcode tool. Here we select the iPhone Six S Plus which can be as follows.

C: \ Users \ mohammad \ Desktop \ select-simulator-in-ios-xcode-project-to-run-app.png

Once we have selected the required simulator; We run our programs using the Run button, as you can see in the figure below; In the upper left corner is the Xcode toolbar.

C: \ Users \ mohammad \ Desktop \ ios-swift-hello-world-run-xcode-application.png

Hello World app output on iOS

When we run the Hello world app on iOS; We receive the output which is as follows.

C: \ Users \ mohammad \ Desktop \ ios-swift-hello-world-app-result.png