<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Dictionary/subscript(_:default:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SD_7defaultq_x_q_yXKtcip"
  },
  "title" : "subscript(_:default:)"
}
-->

# subscript(_:default:)

Accesses the value with the given key, falling back to the given default
value if the key isn’t found.

```
subscript(key: Key, default defaultValue: @autoclosure () -> Value) -> Value { get set }
```

## Parameters

`key`

The key the look up in the dictionary.

`defaultValue`

The default value to use if `key` doesn’t exist in the
dictionary.

## Return Value

The value associated with `key` in the dictionary; otherwise,
`defaultValue`.

## Overview

Use this subscript when you want either the value for a particular key
or, when that key is not present in the dictionary, a default value. This
example uses the subscript with a message to use in case an HTTP response
code isn’t recognized:

```
var responseMessages = [200: "OK",
                        403: "Access forbidden",
                        404: "File not found",
                        500: "Internal server error"]

let httpResponseCodes = [200, 403, 301]
for code in httpResponseCodes {
    let message = responseMessages[code, default: "Unknown response"]
    print("Response \(code): \(message)")
}
// Prints "Response 200: OK"
// Prints "Response 403: Access forbidden"
// Prints "Response 301: Unknown response"
```

When a dictionary’s `Value` type has value semantics, you can use this
subscript to perform in-place operations on values in the dictionary.
The following example uses this subscript while counting the occurrences
of each letter in a string:

```
let message = "Hello, Elle!"
var letterCounts: [Character: Int] = [:]
for letter in message {
    letterCounts[letter, default: 0] += 1
}
// letterCounts == ["H": 1, "e": 2, "l": 4, "o": 1, ...]
```

When `letterCounts[letter, default: 0] += 1` is executed with a
value of `letter` that isn’t already a key in `letterCounts`, the
specified default value (`0`) is returned from the subscript,
incremented, and then added to the dictionary under that key.

> Note: Do not use this subscript to modify dictionary values if the
> dictionary’s `Value` type is a class. In that case, the default value
> and key are not written back to the dictionary after an operation.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)