Sobes.tech
Junior

What data types exist in Swift?

sobes.tech AI

Answer from AI

In Swift, the following main data types exist:

  1. Primitive types:
    • Integer types: Int (default, platform-dependent), Int8, Int16, Int32, Int64, and their unsigned counterparts UInt8, UInt16, UInt32, UInt64.
    • Floating point: Float (32-bit representation), Double (64-bit representation, default).
    • Boolean values: Bool (takes values true or false).
  2. Strings and Characters: String, Character.
  3. Collections:
    • Arrays: Array (ordered collection allowing duplicates).
    • Sets: Set (unordered collection of unique values).
    • Dictionaries: Dictionary (unordered collection of key-value pairs, where keys are unique).
  4. Tuples: Tuple (grouping multiple values of different types into a single composite value).
  5. Optional types: Optional (represents either a value of a certain type or absence of a value (nil)).
  6. Structures: struct (value types).
    struct Point {
        var x: Int
        var y: Int
    }
    
  7. Classes: class (reference types).
    class Person {
        var name: String
        init(name: String) {
            self.name = name
        }
    }
    
  8. Enumerations: enum (group of related values).
    enum Direction {
        case up, down, left, right
    }
    
  9. Function types: (Parameters) -> ReturnType (represent closures and functions).
    let greeting: (String) -> String = { name in
        return "Hello, \(name)!"
    }