Declare Int Variable
To declare a variable of type Int in Swift, use : Int
after the variable name.
var age: Int
Swift Example
Let us write a swift program, in which we declare a variable with the name age
, and declare this variable as type Int
. Later, we will assign a value 25
to this variable and print it.
main.swift
import Foundation
var age: Int
age = 25
print(age)
Program Output
25
The type of the variable age
is Int
, because of which we could assign an Int value to the variable.
We can also programmatically check the type of the variable age
using type(of:)
function.
main.swift
import Foundation
var age: Int
print(type(of: age))
Program Output
Int
Summary
We have learnt how to declare a variable of type Int in Swift.