Sobes.tech
Junior

Mis võib olla sõnastiku võti ja väärtus?

sobes.tech AI

Vastus AI-lt

In iOS (Swift), a Dictionary is a collection of key-value pairs.

  • The key can be any type that conforms to the Hashable protocol. This means the key must be hashable for quick lookup. For example, String, Int, UUID, and other standard types.

  • The value can be any type, including structures, classes, primitives, or even other collections.

Example:

var dict: [String: Int] = ["apple": 3, "banana": 5]

// Key — String (hashable), value — Int

// You can also use custom types if they conform to `Hashable`:
struct User: Hashable {
    let id: Int
    let name: String
}

var userScores: [User: Double] = [:]

It is important that the keys are immutable and hashable for the dictionary to work correctly.