---
title: "subscript(_:)"
framework: foundation
role: symbol
role_heading: Instance Subscript
path: "foundation/nsdictionary/subscript(_:)-1bt1b"
---

# subscript(_:)

Accesses the value associated with a given key.

## Declaration

```swift
@objc dynamic subscript(key: Any) -> Any? { get }
```

## Parameters

- `key`: The key whose value you want to retrieve.

## Discussion

Discussion The returned value is nil if key does not exist in the dictionary. The listing below creates a new dictionary. It prints the value of a key found in the dictionary ("Coral") and a key not found in the dictionary ("Cerise"). var hues = NSDictionary(dictionary: ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]) print(hues["Coral"]) // Prints "Optional(16)" print(hues["Cerise"]) // Prints "nil"

## See Also

### Accessing Keys and Values

- [allKeys](foundation/nsdictionary/allkeys.md)
- [allKeys(for:)](foundation/nsdictionary/allkeys(for:).md)
- [allValues](foundation/nsdictionary/allvalues.md)
- [value(forKey:)](foundation/nsdictionary/value(forkey:).md)
- [objects(forKeys:notFoundMarker:)](foundation/nsdictionary/objects(forkeys:notfoundmarker:).md)
- [object(forKey:)](foundation/nsdictionary/object(forkey:).md)
- [subscript(_:)](foundation/nsdictionary/subscript(_:)-52n56.md)
