What are Swift's basic data types?

Swift offers a variety of basic data types that are used frequently in programming. Understanding these types is essential for effective coding in Swift. Below are some of the primary data types available in Swift:

  • Int: Represents integer values.
  • Float: Represents 32-bit floating-point numbers.
  • Double: Represents 64-bit floating-point numbers.
  • Bool: Represents Boolean values, either true or false.
  • String: Represents a series of characters.
  • Character: Represents a single character.
  • Array: A collection of values (of the same type) in an ordered list.
  • Dictionary: A collection of key-value pairs.

Here's an example of these data types in action:

let age: Int = 30 let pi: Float = 3.14 let precision: Double = 3.14159265359 let isSwiftFun: Bool = true let greeting: String = "Hello, Swift!" let initial: Character = "S" let numbers: [Int] = [1, 2, 3, 4, 5] let person: [String: String] = ["name": "Alice", "city": "Wonderland"]

Swift data types Int Float Double Bool String Character Array Dictionary