Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

Swift - Data Types

Swift is a statically-typed language, which means that the data type of a variable is checked at compile-time. Swift offers a rich set of built-in data types. Here are the primary ones:

1. Basic Data Types:

  • Int: This represents an integer value. It can be further divided into:

    • Int8, Int16, Int32, Int64: Signed integer types with the corresponding number of bits.
    • UInt8, UInt16, UInt32, UInt64: Unsigned integer types.
    var count: Int = 10
    
  • Double and Float: These represent floating-point numbers. Double has more precision compared to Float.

    var distance: Double = 100.5
    var height: Float = 50.2
    
  • Bool: Represents a boolean value, either true or false.

    var isActive: Bool = true
    
  • Character: Represents a single character.

    var letter: Character = "A"
    
  • String: Represents a sequence of characters.

    var name: String = "Alice"
    

2. Collection Types:

  • Array: Ordered collections of values. Arrays are type-safe, and have a mutable variant (if declared with var) and an immutable variant (if declared with let).

    var fruits: [String] = ["apple", "banana", "cherry"]
    
  • Dictionary: Collections of key-value pairs. Like arrays, they can be mutable or immutable based on var or let.

    var scores: [String: Int] = ["Alice": 90, "Bob": 85]
    
  • Set: Unordered collections of unique values.

    var colors: Set<String> = ["red", "blue", "green"]
    

3. Optional Types:

Optionals are a powerful feature in Swift that allows variables to have "no value". This is Swift's way of dealing with the absence of a value, or null in other languages.

var age: Int? = nil

Here, age is an optional Int which currently does not hold any value.

4. Tuples:

Tuples group multiple values into a single compound value. The values within a tuple can be of any type.

let httpStatus = (404, "Not Found")

5. Enumerations:

Enums define a common type for a group of related values, allowing you to work with those values in a type-safe way.

enum Direction {
    case north
    case south
    case east
    case west
}

Conclusion:

Swift's rich set of data types ensures that developers can write expressive and safe code. Type safety and type inference in Swift mean that the compiler can often guess the appropriate type of a variable or constant, but you can always specify it explicitly if needed.

  1. Basic data types in Swift programming:

    • Description: Swift has several basic data types, including Integers (Int), Floating-Point Numbers (Float and Double), Booleans (Bool), and Characters (Character).

    • Code:

      let integerNumber: Int = 42
      let floatingPointNumber: Double = 3.14
      let booleanValue: Bool = true
      let characterValue: Character = "A"
      
  2. Swift numeric data types examples:

    • Description: Numeric data types in Swift include Integers (Int), which represent whole numbers, and Floating-Point Numbers (Float and Double), which represent decimal numbers.

    • Code:

      let integerNumber: Int = 42
      let floatingPointNumber: Double = 3.14
      
  3. String data type in Swift:

    • Description: The String data type in Swift is used to represent text and is enclosed in double quotes.

    • Code:

      let greeting: String = "Hello, Swift!"
      
  4. Swift boolean data type usage:

    • Description: The Bool data type in Swift is used to represent Boolean values, either true or false.

    • Code:

      let isSwiftFun: Bool = true
      let hasTutorials: Bool = false
      
  5. Introduction to Swift collection data types:

    • Description: Collection data types in Swift include Arrays, Sets, and Dictionaries, providing ways to store and organize multiple values.

    • Code:

      // Array
      var numbers: [Int] = [1, 2, 3]
      
      // Set
      var uniqueNumbers: Set<Int> = [1, 2, 3]
      
      // Dictionary
      var userScores: [String: Int] = ["Alice": 95, "Bob": 80]
      
  6. Working with arrays in Swift:

    • Description: Arrays in Swift are ordered collections of values of the same type.

    • Code:

      var fruits: [String] = ["Apple", "Banana", "Orange"]
      
  7. Enumerations and custom data types in Swift:

    • Description: Enumerations (enum) in Swift allow you to define your custom data types with a set of possible values.

    • Code:

      enum CompassDirection {
          case north, south, east, west
      }
      
      let direction: CompassDirection = .north