Contents

About Swift Syntax 2 (Type)

   Jan 16, 2024     2 min read

About “Swift Grammar 2 (Type)”

I had the opportunity to study iOS this time, and I’ve been studying grammar, so I thought I’d write a post to summarize it.

There are various types in Swift.

In this article, I will introduce the types.

  1. Integers Represents an integer type, with signed Int and unsigned UInt. Examples
var positiveNumber: Int = 42
var unsignedNumber: UInt = 7
```swift

2. Floating-Point Number
   Represents a real number and has types Float and Double.
   Examples

```swift
var floatNumber: Float = 3.14
var doubleNumber: Double = 3.1415926535
  1. String A string type that represents text. Examples
var string: String = "Hello, Swift!"
  1. Booleans Represent true or false. Example.
var isTrue: Bool = true
var isFalse: Bool = false
```swift

5. Tuples
   A type that allows you to group multiple values together.
   Example.

```swift
var person: (String, Int) = ("John", 35)
```swift

6. Optional
   A type to represent situations where a value may or may not be present.
   Example.

```swift
var optionalString: String? = nil
optionalString = "Now it has a value"
  1. Array A collection type that stores multiple values of the same type in order. Example
var fruits: [String] = ["Apple", "Banana", "Cherry"]
  1. Dictionary A collection type that stores data in key-value pairs. Examples
var ages: [String: Int] = ["John": 28, "Sarah": 25]
  1. Set A collection type that stores multiple values of the same type without duplicates. Example.
var uniqueNumbers: Set<Int> = [1, 2, 2, 3, 3, 3, 3]
// uniqueNumbers is now {1, 2, 3}
  1. Enumerations A type that works with groups of related values.
enum CompassPoint {
    case north
    case south
    case east
    case west
}
var direction: CompassPoint = .north