This topic is about Swift – Variables.
A variable provides us with named storage that our programs can manipulate. Each variable in Swift 4 has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Swift 4 supports the following basic types of variables β
- Int or UInt β This is used for whole numbers. More specifically, you can use Int32, Int64 to define 32 or 64 bit signed integer, whereas UInt32 or UInt64 to define 32 or 64 bit unsigned integer variables. For example, 42 and -23.
- Float β This is used to represent a 32-bit floating-point number. It is used to hold 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”
Swift 4 also allows to define various other types of variables, which we will cover in subsequent chapters, such as Optional, Array, Dictionaries, Structures, and Classes.
The following section will cover how to declare and use various types of variables in Swift 4 programming.
Variable Declaration
A variable declaration tells the compiler where and how much to create the storage for the variable. Before you use variables, you must declare them using var keyword as follows β
var variableName = <initial value>
The following example shows how to declare a variable in Swift 4 β
var varA = 42 print(varA)
When we run the above program using playground, we get the following result β
42
Type Annotations
You can provide a type annotation when you declare a variable, to be clear about the kind of values the variable can store. Here is the syntax β
var variableName:<data type> = <optional initial value>
The following example shows how to declare a variable in Swift 4 using Annotation. Here it is important to note that if we are not using type annotation, then it becomes mandatory to provide an initial value for the variable, otherwise we can just declare our variable using type annotation.
var varA = 42 print(varA) var varB:Float varB = 3.14159 print(varB)
When we run the above program using playground, we get the following result β
42 3.1415901184082
Naming Variables
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Swift 4 is a case-sensitive programming language.
You can use simple or Unicode characters to name your variables. The following examples shows how you can name the variables β
var _var = "Hello, Swift 4!" print(_var) var δ½ ε₯½ = "δ½ ε₯½δΈη" print(δ½ ε₯½)
When we run the above program using playground, we get the following result.
Hello, Swift 4! δ½ ε₯½δΈη
Printing Variables
You can print the current value of a constant or variable with the print function. You can interpolate a variable value by wrapping the name in parentheses and escape it with a backslash before the opening parenthesis: Following are valid examples β
var varA = "Godzilla" var varB = 1000.00 print("Value of \(varA) is more than \(varB) millions")
When we run the above program using playground, we get the following result.
Value of Godzilla is more than 1000.0 millions
In thos topic we learned about Swift – Variables. To learn more, Click Here.
Pingback: Swift - Data Types - Adglob Infosystem Pvt Ltd
I truly appreciate this article post.Really thank you! Will read on…