Sobes.tech
Intern

What are data types and which of them exist?

sobes.tech AI

Answer from AI

Data types define a set of possible values and operations that can be performed on them. In Swift, they are divided into Value Types and Reference Types.

Value Types:

  • Values are copied when assigned or passed to a function.
  • Each variable or constant holds its own independent copy of data.
  • Changes to one copy do not affect others.

Examples of Value Types:

  • Structures (struct)
  • Enumerations (enum)
  • Tuples (tuple)
  • Basic types: Int, Double, Bool, String, arrays (Array), dictionaries (Dictionary), sets (Set).
// Example of Value Type: struct
struct Point {
    var x: Int
    var y: Int
}

var p1 = Point(x: 1, y: 2)
var p2 = p1 // p2 gets a copy of p1
p2.x = 10 // Changing p2 does not affect p1

print("p1: \(p1)") // p1: Point(x: 1, y: 2)
print("p2: \(p2)") // p2: Point(x: 10, y: 2)

Reference Types:

  • Values are not copied. When assigned or passed to a function,
  • a reference to the same data instance in memory is passed.
  • Multiple variables or constants can refer to the same instance.
  • Changes through one reference are visible through all other references.

Examples of Reference Types:

  • Classes (class)
  • Functions (func)
  • Closures (closure)
// Example of Reference Type: class
class Circle {
    var radius: Double

    init(radius: Double) {
        self.radius = radius
    }
}

var c1 = Circle(radius: 5.0)
var c2 = c1 // c2 references the same instance as c1
c2.radius = 10.0 // Change in c2 affects c1

print("c1 radius: \(c1.radius)") // c1 radius: 10.0
print("c2 radius: \(c2.radius)") // c2 radius: 10.0

Table of main types:

Category Type(s) Value/Reference Type Description
Integer types Int, UInt, Int8, UInt8, etc. Value Whole numbers of various sizes, signed and unsigned
Floating point Float, Double Value Numbers with fractional parts
Boolean Bool Value Logical values (true or false)
Strings String Value Sequence of characters
Collections Array, Dictionary, Set Value Ordered/unordered collections of same-type elements
Structures struct Value Composite user-defined types holding related values
Enumerations enum Value Define a common type for a group of related values
Tuples (value1, value2, ...) Value Group multiple values into a single composite type
Classes class Reference Composite user-defined types supporting inheritance and reference behavior
Functions () -> Void, (Int) -> String, etc. Reference Blocks of executable code
Closures { (parameters) -> return type in code } Reference Self-contained blocks of code that can be passed around and used
Optionals Type? (syntactic sugar for Optional<Type>) Value Represents either a value of type Type or its absence (nil)

Choosing between Value and Reference Types depends on the task: Value Types are preferable for small data and thread safety, while Reference Types are suitable for complex objects requiring shared state and inheritance.

What are data types and which of them exist? — iOS - sobes.tech