Data Types

There are six basic types of data types in Swift programming.

  • Character
  • String
  • Integer
  • Float
  • Double
  • Boolean

Character

The character data type is used to represent a single-character string. We use the Character keyword to create character-type variables. For example

String

The string data type is used to represent textual data. We use the String keyword to create string-type variables.

String Interpolation

It allows injecting an expression directly into a string literal. This can be done with all types of values, including strings, integers, floating point numbers and more.

The syntax is a backslash followed by parentheses wrapping the value: \\(value). Any valid expression may appear in the parentheses, including function calls.

let number = 5

let interpolatedNumber = “\\(number)” // string is “5”

let fortyTwo = “\\(6 * 7)” // string is “42”

Concatenate strings

Concatenate strings with the + operator to produce a new string:

let name = “John”

let surname = “Appleseed”

let fullName = name + ” ” + surname // fullName is “John Appleseed”

Reversing Strings

let aString = “This is a test string.”

let reversedCharacters = aString.characters.reversed()

let reversedString = String(reversedCharacters)

Integer

Integer refers to a category of data types representing whole numbers. It’s a protocol that defines common behavior for integer types. Swift provides several built-in integer types, such as Int, Int8, Int16, Int32, Int64, UInt, UInt8, UInt16, UInt32, and UInt64

var integerNumber: Int = 10

var unsignedIntegerNumber: UInt = 20

Leave a Reply

Your email address will not be published. Required fields are marked *