Table of contents
1.
Introduction
2.
Variables
3.
Variable Declaration
4.
Type Annotations
5.
Naming Variables
6.
Variable Concatenation and Interpolation
7.
Printing Variables
8.
Frequently Asked Questions
8.1.
What is Swift suitable for?
8.2.
Is Swift a front-end or a back-end language?
8.3.
How many different types of integers are there in Swift?
8.4.
In Swift, how do you split a string?
9.
Conclusion
Last Updated: Mar 27, 2024

Swift Variables

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Swift is a programming language for developing apps for iOS, macOS, watchOS, and tvOS. It was first released in 2014 with a few revisions over the years. Swift has its implementations of all essential C and Objective-C types.

Swift makes effective use of variables. Swift, like C, employs variables to store and refer to values by their names. 

This article will discuss the Swift variables and their declaration, type annotation, variables catenation, and interpolation with examples.

Variables

Variables are used in Swift to store and change data by executing various operations dependent on our needs. We can store Integers, strings, characters, bools, and other data in variables.

When we declare a variable in the Swift programming language, the required space in computer memory is automatically reserved for that variable.

The space allocation in memory varies depending on the variable type, such as integer, float, or double.

Swift allows us to declare several sorts of variables such as ArraysDictionaries, etc.

The primary and most commonly used data types with variables in Swift programming language are listed below.

Variable Declaration

We must first declare variables in any programming language before using them in our programmes. We define variables in Swift using the "var" keyword, and we can alter the values of variables throughout the programme based on our needs.

The syntax for declaring the variable is as follows in a swift programming language.

var <variable_name> = <value>

For example,

var str = “Hello!!”
print(str)

The first line of the following code informs the compiler that we have defined a variable "str" and assigned its value; by doing so, the compiler will allocate the necessary space in our computer memory to keep the variable value. The second line will print the variable name.

Following is the output of the above swift variable declaration program.

Hello!!

Type Annotations

We can quickly determine what kind of values the variable can store in Swift by defining type annotation while declaring it.

We can define variable type annotation by following the variable name with a colon, a space, and the type's name.

The syntax for adding a type annotation to a variable in Swift is as follows.

var variable_name: <data type> = <Initial value>

Here's a simple example of applying a type annotation to a constant.

var msg: String  = "Welcome to Coding Ninjas."
print(msg)

We defined the variable "msg" and its data type (String) in the preceding example. The swift programme above will return the value given below.

Welcome to Coding Ninjas.

When we declare a constant with a type annotation, we must specify that the constant's initial value matches its data type.

Naming Variables

Swift's variable names can contain nearly any character, including strings, numbers, underscores, and Unicode characters. Still, they should not contain whitespace, mathematical symbols, arrows, or erroneous Unicode code points.

The constant name should not begin with a number. However, numbers may appear elsewhere in the name. The following is a simple example of naming constants in Swift.

var exm1 = "Hello!!"
print(exm1)

var _exm = "Ninja."
print(_exm)

The result of the above code will be:

Hello!!
Ninja.

Variable Concatenation and Interpolation

In Swift, we can combine two variables to form a single variable in two ways:

  1. By Concatenation
  2. By Interpolation

Following is an example of concatenating two variables into a single variable:

var str1 = "Hello!!"
var str2 = "Ninja."
Let result = str1 + str2
print(result)

The output of the above program is shown below:

Hello!! Ninja.

We'll see how to utilise interpolation to mix two variables with examples. In Swift, here's a simple example of combining two variables using interpolation.

var str1 = "Hello!!"
var str2 = "Ninja."
print(" / (str1) / (str2)" )

The output of the above program is shown below:

Hello!! Ninja.

Printing Variables

The "print" function in Swift can be used to print variables. A basic example of printing variables follows.

var str1 = "Printing"
var str2 = "Variables."
Let result = str1 + str2
print(result)

The output of the above program is shown below:

Printing Variables.

Based on our needs, this is how we can define, declare, and use variables in a swift programming language.

Frequently Asked Questions

What is Swift suitable for?

Swift is an Apple-developed programming language for developing iOS, Mac, Apple TV, and Apple Watch programs. It's intended to allow developers more flexibility than ever before. Swift is simple to use and open-source, allowing anyone with an idea to make something unique.

 

Is Swift a front-end or a back-end language?

Both are correct. Swift can be used to create client-side (frontend) and server-side (backend) software.

 

How many different types of integers are there in Swift?

Swift presently has ten different numeric types. There are four signed integer types of various bit sizes and their unsigned equivalents: Int8 and UInt8.  Int16 and UInt16.

 

In Swift, how do you split a string?

In Swift, use String.split() function to split a String by a character separator. Call the split() function on the String and send the separator (character) as a parameter to the split() function. split() returns a String Array that contains the splits as elements.

Conclusion

This article extensively discussed Swift Variables, their declaration, how to name them, Concatenation and Interpolation of Variables, and printing their results with examples. Check out the definitive swift tutorial for beginners to know more about Swift.

We hope this blog has helped you enhance your knowledge regarding Variables in Swift. You can learn more about IOS by visiting the Ios and os related articles in CodingNInjas Blogs.  

Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck! Keep coding and keep reading Ninja!!

Live masterclass