Variables and Constants in Swift – 1


By reading this article your following problems will be answered.

How to define variables and constants in Swift.

How to define variables and constants in Swift.

How to define constants in Swift.

What are the data types of Swift.

How to use multiple data types in Swift.

Optional types in Swift.

How to declare multiple variables or constants in a single line in Swift.


  • Let’s discuss some basics about Swift variables and constants.

Variables

  • ‘var’ is used to define variables.
  • All the variables are required to be defined prior to use.
  • Variables can be defined in following ways.
1. var result: Int
2. var number1: Int = 10
3. var number2 = 80
  • A variable can be defined by just mentioning the data type as in the line 1.
  • Also we can specify the value of the respective variable after defining the type, as in the line 2
  • Else, as in the line 3, we can just define the variable by giving it an initial value, so it can automatically identify the variable’s data type by referring to the given initial value.

Constants

  • ‘let’ is used to define constants.
  • As variables, constants are also need to be defined prior to use.
  • A constant can be defined in following ways,
let label: String = "The language is "
let language = “swift"
  • We can call the constant name to retrieve the values stored in the constant like we do with a variable.
  • But we can’t change the value of a constant.

Data types

  • There are many data types, main data types are as follows,
Data typeDecription
CharacterA single character.
StringText
IntA whole number. (Not a decimal.)
FloatA 32-bit floating point number. (That means a 32-bit decimal number.)
DoubleA 64-bit floating point number. (That means a 32-bit decimal number.)
BoolJust true of false
Swift data types

Using multiple data types

  • To convert the data type of the value, we need to as follows.
let version: Int = 1
String(version)
  • To combine variables or constants with different data types, all the variables and constants need to be the same data type, otherwise we have to convert the data type as below.
  • To use multiple data types at once, we can either do as follows.
let version: Int = 1
let label = “Version is “
var message = label + String(version)
  • Or we can include the value with another data type in parentheses and add a back slash before them.
let version: Int = 1
let label2 = "Version is "
var message = label2 + "\(version)"
  • Also we can do calculations inside those parentheses.
let number1 = 200
let number2 = 300
var result = "Result is: + \(number1 + number2) "
  • But make sure to contain this [\()] in quotation marks.

Optional types

  • There is a thing called optional types in Swift.
  • Optional types either output the value or says there isn’t value at all.
  • Optional values can be used as follows.
let FirstName: string? = “Joe”
let  SurName: string? = nil
  • ‘nil’ means no value at all.

Additional points

  • Multiple variables and constants can be declare by separating with commas.
var num1 = 100, num2 = 200, num3 = 300
var cat1, cat2, cat3: String

Click here to know more about Swift


To learn more and access more content about Swift

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/