mac 1108183 1920

Make apps with Swift 5 on your Mac

mac 1108183 1920

Swift is a programming language used to write applications and games for the iPhone, iPad, Mac, Apple Watch and more. Apple has clearly designed Swift to achieve the fastest and most efficient performance of the device, and the Swift 5 extends the already impressive set of features.

In this article, we show how to use Swift 5, explain that if you have all the new features of this new version of programming knowledge and language, then why should you do this?

Overview of Swift 5

Swift 5 is a programming language developed by Apple, which allows developers to create applications for iOS, MacOS, TVOS, and WatchOS.

The fifth edition was released in March 2019 with Xcode 10.2. Here we are going to talk about the new features of Swift 5, but you should know that Swift has been in the market for some time: Swift 1.0 was launched in September. 2014. According to Apple, Swift is 2.6 times faster than Objective C and is 8.4 times faster than Python.

Why should you develop with Swift 5?

1) Swift is open source. This means that the source code behind the program or programming language is available to the general public. Developers can inspect, modify and deploy the program wherever you want.

Apple has stated in the open source page: “Apple believes that the use of open source functionality makes macOS a stronger and safer operating system, because its main components are subject to strict revision control for decades.”

2) Learning Swift is easy. Apple developed its language to be easy to use. To begin, it is an ideal language for beginners, because syntax is easy to understand. If you have already developed the software, you will find that Swift’s syntax and concepts are similar to those you already use.

3) Swift is fast. Apple claims that Swift Search algorithm is 2.6 times faster than Objective-C and up to 8.4 times faster than Python.

4) Swift is safe. You will see that it uses modern programming commands which allows to maintain additional level of security, necessary for the development of new applications.

5) Playgrounds. Xcode comes with a function called ‘Playground’ where Swift 5 programmer can write code and see results immediately.

6) Swift 5 is designed for the future by allowing it to be developed for different platforms: iOS, MacOS, WatchOS and TVOS.

7) Swift improves with each update. Swift 5 brought long-awaited ABI stability, which means that future swift compilers will be able to compile swift 5 and later codes, and migration will be less expensive in new versions of Swift.

It also means that to make it with all applications made with Swift 5 and newer versions, operating system providers can integrate Swift Standard Library into the operating system.

Swift 5 concepts (Basic and Advanced)

Use ‘let’ to make a constant and ‘var’ to define a variable. The value of a constant can not be changed once assigned; The value of a variable can change. Users do not always have to write explicitly. Providing a value when you create a constant or variable allows the compiler to interact:

Come on constVar = 42 

var numberVar = 27

The developer can also specify the type. In the following example, we are declaring an integer.

var numberVar: Int = 27

Comments on Swift

The comments in Swift can be of two types.

Single line:

// This is a comment

Multiple line comments:

/ * This is a 

multiline comment * /

Decision making in Swift

The syntax of an if statement in Swift is as follows:

yes boolean_expression { 

/ * statements will be executed if the Boolean expression is true * / 


For example:

foto 10

The syntax of an if … else statement in Swift 5 is as follows:

yes boolean_expression { 

/ * statements will be executed if the Boolean expression is true * / 

} on the contrary { 

/ * statements will be executed if the boolean expression is false * / 


For example:

foto 11

The syntax of an if … else if … else statement in Swift 5 is as follows:

if boolean_expression_1 { 

/ * Executes when the Boolean expression 1 is true * / 

} else if boolean_expression_2 { 

/ * It is executed when the expression boolean 2 is true * / 

} else if boolean_expression_3 { 

/ * It is executed when the Boolean expression 3 is true * / 

} else { 

/ * It is executed when none of the previous conditions is true * / 


For example:

foto 12

Change the declaration

Below is a generic syntax of the Switch instruction available in Swift 5. Here, if the step function is used, it will continue with the execution of the next case and then exit the Switch instruction.

Change expression { 

case expression1: 

sentence (s) 

fallthrough / * optional * / 

case expression2, expression3: 

sentence (s) 

fallthrough / * optional * / 

default: / * Optional * / 

statement (s); 


For example:

foto 13


Create matrices and dictionaries using brackets, that is, [and], and access their elements by typing the index or key in brackets. The next line creates a matrix.

var arrayList = [“Swift”, “JavaScript”, “Java”, “PHP”]

To access and modify the second element of a matrix, we can write directly:

arrayList [2] = “React Native”

To create an empty matrix, use the initializer syntax.

var emptyArray = [String] () 

emptyArray = []

The dictionaries

occupations var = [“Steve”: “Developer”, “Kate”: “Designer”,]

To access and modify any value for a dictionary we can write directly:

Occupations [“Steve”] = “CTO”

To create an empty dictionary, use the initializer syntax.

occupations = [:]


Sets in Swift are similar to arrays, but only contain unique values.

var a: Set = [1,2,3,4,5,6,7,8,9,0]

Swift also introduces the Optionals type, which handles the absence of a value. The optionals say that “there is a value and it is equal to x” or “there is no value at all”. You can define an optional with ‘?’ or ‘!’

var myString: String?

‘?’ It means that the value may be present or absent.

‘!’ means that the value may be null initially, but in the future, when it is used, it must have a value, or it will generate a runtime error.

No sign means that the variable is not optional and must be assigned a value, or it will generate a compilation error. 


Below is the syntax for creating a function in Swift: inputNum is the name of the parameter followed by the DataType, ‘createStr’ is the name of the function. ‘-> String’ denotes the type of return. The function takes Integer as input and converts it to String and returns it.

func createStr (Number inputNum: Int) -> String 

return “\ (inputNum)” 


The function can be called using the following syntax:

createStr (Number: 345)


The syntax for creating a Class Car is shown below. It has an optional member variable numOfPersons and a displayDetails () function

class Car 

var numOfPersons: Int? 

func displayDetails () { 


The instance of the class can be created using the following line:

var myCar: Car = Car ()

The variable ‘numOfPersons’ can be initialized as follows:

myCar.numOfPersons = 5

foto 14


In Swift we can extend the functionality of an existing class, structure or type of numbering with the help of extensions. The functionality can be added with extensions, but in this way it is not possible to cancel the functionality.

In the following example we have a car class and we are adding an extension to the car to add another property. By accessing the property ‘speed’, it can be accessed directly as if it belonged to the class.

foto 15


The Tuple type is used to group multiple values ​​into a single composite value. Here is the syntax of a Tuple declaration:

var TupleName = (Value1, value2, … any number of values)

Here is the Tuple statement:

var error501 = (501, “Not implemented”)

New features in Swift 5

Swift 5 brings us the raw wire, a feature that facilitates the creation of strings, which include quotes and backslashes without the need to use the escape sequences in the form of previous versions of Swift.

In the crude strings, inverted commas and bars are literally interpreted as symbols that are interpreted as string or escape termination characters.

To use strings, add # at the beginning and end of the string:

foto 16

Because the backslash in the raw strings is interpreted as a symbol, to use the string interpolation in the raw strings another # must be added after the backslash symbol:

foto 17

If you need to use the sequence “# # together inside a raw string, you must add # to the beginning and end of the string, as shown in the following image.

foto 18

Search for whole multiples

In Swift 4.2 and earlier versions, it was necessary to use the operator (%) to know if a number was a multiple of the other. Now in swift 5, there is a dedicated way for him, which makes the code very clear:

foto 19

Handling future enumeration cases

Swift instructions must always be exhaustive. This means that you always have to handle all cases of enumeration or handle only specific cases with the addition of the default case that handles the other cases:

foto 20

The problem with this approach is that if in the future the developer decides to add another case to the list, there will be no warning from the compiler that the new case was added. This means that the new case will be handled as a default case, which is not always desirable. 

To solve this problem, a new attribute @nown in Swift 5 was added. This attribute is used together with the default case.

foto 21

With the case @ unknown by default in the switch statement, the compiler will issue a warning if a new case was added to the list in the future. In this way, the developer can decide whether to handle the new case or not.

Optional nested

Nested option codes can be created by handling those you use by trial? In Swift 5, nested alternate becomes a regular alternative. It matches the behavior of conditional and alternative chaining.

foto 22

In the previous example, you can see that the model variable is of type String? and not String ?? as in Swift 4.2.

Type of result in the Standard Library

In Swift 5, the ‘result‘ type was added to the standard library. The type ‘result’ provides a clean and simple way to handle errors in asynchronous code. It is implemented as a list of success stories and failures.

Both cases are implemented using generics. In the case of success, there can be any type of affiliated value, while in case of failure, there should be an associated value that corresponds to the error protocol. Here is an example that shows the use of the result type:

foto 23

In the previous example, we have implemented a simple ApiClient that gets names from a URL. Note that the second parameter of the fetchNames function is an end closure that accepts a type of ‘Result’. The type ‘Result’ in our example uses [String] for the success case and ApiError for the case of failure.

Now, we can use the code above like this:

foto 24

The best places to learn programming with Swift 5

Do you need to learn Apple’s Swift 5 development language? We help you with a series of tips and resources of the network, and we include free and paid full courses. 

How to write a simple application in Swift

Open Xcode and select ‘File> New> Project’. Select the single view application from the list of templates.

foto 2

Enter a name for your application or project as well as the name of the Organization (may be the name of your company). The identifier of the organization is usually the URL of your company in reverse order, for example, com.mycompany.myapp.

Select Swift as the language and click on ‘Next’. Finally, select the location on your Mac where you want Xcode to create the project. 

foto 3

Once the project is created, the system will show you the following screen:

foto 4

Here, you can develop a simple application, where the user can enter their name in a text field and receive a simple greeting when they press a button.

To begin, you should go to the Main.storyboard file in the left panel. You will see an empty view appear. Click on the round button in the upper right corner to open the object library. Drag a text field, label and put the button in view.

foto 5

Select the button in the view hierarchy and set the title to ‘Generate greeting’ within the utilities area shown on the right side. 

foto 6

Double-click on the ViewController.swift file. A separate window will open.

Now select the text field in the view and hold down the ‘Ctrl‘ key to drag to the top of ViewController. You will be asked to create an IBOutlet for the text field. Call it ‘textField‘.

foto 7

Do the same with the label and name it IBOutlet. You will also have to do the same with the button, but instead of dragging it to the top of the ViewController, drag it to the bottom to create an IBAction method. Name the method ‘buttonTapped‘.

The IBOutlets are used to access the controls in the script of our code and the IBAction methods are used to react to button events such as beats.

Add the following code in the buttonTapped method of your ViewController class:

if you leave name = textField.text { 

self.label.text = “Hello” + name 


This is how the ViewController.swift file should look after these changes:

foto 8

You are ready to run the application. Press the ‘Run‘ button and the application will start in the simulator.

When the user adds their name in the text field and touches the button, the label at the bottom will show ‘Hello‘ along with the name entered.

foto 9

read more article

0 0 vote
Article Rating

Written by

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x