let a = 50
var b = 10
This code is read as a is constant and holds 50 and b is a variable and holds 10. The value of a can’t be changed whereas b can have different value later on. The type of b is integer, it is inferred from the type of value assigned in it. Since, 10 is integer so the type of b is integer.
You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store. Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use. For example
var myNameIs: String
The colon in the declaration represents “…of type…,”. The code is read as myNameIs of type String.
Now myNameIs variable can store and String type like
You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:
var x, y, z: Int
it is rare that you give the type annotations to variables in swift. Generally, type is inferred from the value assigned in. You can have details of the same at Type Safety and Type Inference.
How to Print Variable, Constants
You can print the current value of a constant or variable with the
print(name) output : AKG
print(anynumber) output: 20
print(_:separator:terminator:) function has three arguments, the variable or constant you want to print, separator and terminator. Last two are optional. if you write this statement
it means you have passed one argument to print and it prints Hello world! with new line on Xcode’s console pane. If you run the following line of code in Xcode
print(“Red”, “Blue”, “White”,”Black”, separator: “,”, terminator: “\n”)
The output would be
This is because separator is ‘,’ and terminator is new line. The default value of terminator is new line. If you rewrite the same code in this way and run
print(“Red”, “Blue”, “White”,”Black”, separator: “\n”)
The output would be as follows
This happens because the separator between two values is new line.
Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable. Wrap the name in parentheses and escape it with a backslash before the opening parenthesis:
print(“The number i am going to print is \(x). “)
The output would be
The number i am going to print is 20.
Another example is
print(“Hello friends my name is \(name) and my age is \(age). “)
In this example, output is
Hello friends my name is nail and my age is 30.
You can put single line comment using // and multiple lines of comment using /* hello */.
// This is a comment
/* This is multiple lines of comments
You need not to put semicolon at the end of each statement. Swift has property, if new line is there at the end of statement it consider end of the statement. But if you wish to write multiple statements in a line you need to put semicolon to separate the statements.
var x=100 // I have not put semicolon at the end of statement it is fine.
var y=20; var z=40; var s=50 // This is an example of having
// semicolons in a line.
All the above concept is implemented in swift on Xcode and available on my GitHub account. The link is here
Integers and Floating-Point Numbers
Integers are whole numbers with no fractional component. It is either signed or unsigned. For example -10, 30 are signed and unsigned respectively . Swift provides signed and unsigned integers in 8, 16, 32, and 64 bit forms. These integers follow a naming convention similar to C, in that an 8-bit unsigned integer is of type
UInt8, and a 32-bit signed integer is of type
Int32. Like all types in Swift, these integer types have capitalized names.
Integer has its bounds (Minimum and Maximum). The min and max properties are used to find the minimum and maximum. For instance:
let theMinSizeOfIntegerIs= Uint8.min // theMinSizeOfIntegerIs=0
let theMaxSizeOfIntegerIs= Uint8.max //theMaxSizeOfIntegerIs=255
In most cases, you don’t need to pick a specific size of integer to use in your code. Swift provides an additional integer type,
Int, which has the same size as the current platform’s native word size:
On a 32-bit platform,
Int is the same size as
On a 64-bit platform,
Int is the same size as
Unless you need to work with a specific size of integer, always use
Int for integer values in your code.
Like Int, swift provides UInt for unsigned integer.
Floating-point numbers are numbers with a fractional component, such as 4.2345,
Swift provides two signed floating-point number types:
Double has a precision of at least 15 decimal digits, whereas the precision of
Float can be as little as 6 decimal digits
Type Safety and Type Inference
Swift is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code expects a
String, you can’t pass it an
Int by mistake.
Because Swift is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.
If you write like this
Here, type of k is not mentioned but swift has property to infer the type of k (any constant or variable) using the type of literal. Therefore, type of k is Int.
If you write like this
In this case, type of z is double because swift always infer double when floating point literal is assigned.
- If you combine integer and floating-point literals in an expression, a type of
Double will be inferred from the context
let Pi = 3 + 0.14159
//Pi is also inferred to be of type Double
2. A literal value is a value that appears directly in your source code, such as 12 and
Integer literals can be written as:
A decimal number, with no prefix
A binary number, with a
An octal number, with a
A hexadecimal number, with a
Floating-point literals can be decimal (with no prefix), or hexadecimal (with a 0x prefix).
When arithmetic operation are performed on constant and variable, it requires that their types must be same. For instance
now x can be used in place of UInt8.
The type of p is now UInt8.
access the individual element values in a tuple using index numbers starting at zero:
print(” the first element’s value is \(x.0) and second element’s value is \(x.1))
It is possible to decompose the tuples value in separate constant or variables. For example
Any permutation of types for tuples can be written like (Int, Int, String), (String, Int, Int, String). Tuples are particularly useful as the return values of functions.