Swift Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

SWIFT TUTORIAL

Agenda

1 Introduction 7 Operators

2 Download and Installation 8 Practical

3 First program 9 Optional Types

4 Comments 10 Conditional Constructs

5 Datatypes

6 Variables

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction
• Swift is a general purpose programming language for developing iOS applications. It is developed by Apple
Inc.

• It is powerful and intuitive language which is easy to learn.

• Swift is a fantastic way to write software, whether it’s for phones, desktops, servers, or anything else that
runs code.

• Swift follows Objective-C runtime library which allows C, Objective-C, C++ and Swift code to run within one
program.

• Swift 5.4 is the latest version of Swift.

• The compiler is optimized for performance and the language is optimized for development, without
compromising on either.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction
Swift defines away large classes of common programming errors by adopting modern programming patterns:

• Variables are always initialized before use.


• Array indices are checked for out-of-bounds errors.
• Integers are checked for overflow.
• Optional ensure that nil values are handled explicitly.
• Memory is managed automatically.
• Error handling allows controlled recovery from unexpected failures.

Swift code is compiled and optimized to get the most out of modern hardware. The syntax and standard library
have been designed based on the guiding principle that the obvious way to write your code should also
perform the best.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation

There are mainly two things to download when you want to work with Swift in windows:

1. Swift installer for Windows


2. Visual Studio 2019 Community

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation
Go to swift.org – the official website of Swift and download the windows tool chain of it.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation
Go to https://visualstudio.microsoft.com/downloads/ – the official website of Visual studio and download the
Community edition of it.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation
Start the visual studio installer and complete some necessary steps while installing:

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation
This is how the visual studio window will appear when it will be downloaded and installed:

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation
Tick “Desktop development with C++”.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation
Once all requirements are ticked, we can start Installing our visual studio:

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation

Now, its time to open swift installer and install swift in our system.

In order to make the Windows SDK accessible to Swift, it is necessary to deploy a few files into the Windows
SDK. The following will modify your Visual Studio Installation, and as such will require to be run from an
(elevated) “Administrator”

Because it is installing the files into the Visual Studio image, the files will need to be copied each time Visual
Studio is updated.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation

Copy these with copy command and run it in the command prompt:

%SDKROOT%\usr\share\ucrt.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\ucrt\module.modulemap"
%SDKROOT%\usr\share\visualc.modulemap "%VCToolsInstallDir%\include\module.modulemap"
%SDKROOT%\usr\share\visualc.apinotes "%VCToolsInstallDir%\include\visualc.apinotes"
%SDKROOT%\usr\share\winsdk.modulemap "%UniversalCRTSdkDir%\Include\%UCRTVersion%\um\module.modulemap"

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Download and Installation

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
First Program

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
First Program

- Environments variables for the Swift compiler:


set SWIFTFLAGS=-sdk %SDKROOT% -resource-dir
%SDKROOT%\usr\lib\swift -I %SDKROOT%\usr\lib\swift -L
%SDKROOT%\usr\lib\swift\windows

- Building a Swift program:


swiftc %SWIFTFLAGS% -emit-executable -o First.exe First.swift
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Comments

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Comments
The first way to write a comment is:

// This is a comment. It is not executed.

This is a single line comment. You could stack these up like so to allow you to write paragraphs:

// This is also a comment.


// Over multiple lines.

However, there is a better way to write comments that span multiple lines:

/* This is also a comment.


Over many...
many...
many lines. */

This is a multi-line comment. The start is denoted by /* and the end is denoted by */.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Datatypes

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Datatypes
• Int or UInt − This is used for whole numbers.
Use Int32, Int64 - 32 or 64 bit signed integer
whereas UInt32 or UInt64 - 32 or 64 bit unsigned integer variables.

• Float − This is used to represent a 32-bit floating-point number and numbers with smaller decimal points. For
example, 3.14159, 0.1, and -273.158.

• Double − This is used to represent a 64-bit floating-point number and used when floating-point values must be
very large. For example, 3.14159, 0.1, and -273.158.

• Bool − This represents a Boolean value which is either true or false.


• String − This is an ordered collection of characters. For example, "Hello, World!"
• Character − This is a single-character string literal. For example, "C"
• Optional − This represents a variable that can hold either a value or no value.
• Tuples − This is used to group multiple values in single Compound Value.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables

• var variableName = <initial value>

• var siteName:String
siteName = “great learning"
print(siteName)

• var siteName = “great learning"


siteName = “great learning academy"
print(siteName)

• var varA = “Swift"


var varB = 3
print("The course duration of \(varA) is \(varB) months.")

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators
• Assignment Operator
• Arithmetic Operators
• Remainder Operator
• Unary Minus Operator
• Unary Plus Operator
• Compound Assignment Operators
• Comparison Operators
• Ternary Conditional Operator
• Range Operators
• Logical Operators

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Practical

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Optional Types

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Conditional Constructs

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Thank You

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

You might also like