Swift - Declare a Variable

Introduction

To declare a variable, you use the var keyword:

Demo

let radius = 3.45
var myAge = 25//from   w w w .j a  va2 s. c  om
var circumference = 2 * 3.14 * radius

print(radius)
print(myAge)
print(circumference)

Result

Once a variable is created, you can change its value:

Demo

let diameter = 20.5
var radius = 10.1
var circumference = 2 * 3.14 * radius
circumference = 2 * 3.14 * diameter/2//  ww  w.  j ava2s .  com
print(circumference)

Result

In the following example, you explicitly use the String() initializer to convert the value of myAge to a string value before concatenating it with another string:

Demo

var myAge = 25
var strMyAge = "My age is " + String(myAge)
print(strMyAge)

Result

Related Topic