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

# subscript(_:)

Returns the value associated with a given key.

## Declaration

```swift
subscript(key: any NSCopying) -> Any? { get }
```

```swift
subscript(key: any NSCopying) -> Any? { get set }
```

## Parameters

- `key`: The key for which to return the corresponding value.

## Return Value

Return Value The value associated with key, or nil if no value is associated with aKey.

## Discussion

Discussion This method has the same behavior as the object(forKey:) method. You shouldn’t need to call this method directly. Instead, this method is called when accessing an object by key using subscripting. id value = dictionary[@"key"]; // equivalent to [dictionary objectForKeyedSubscript:@"key"]

## 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(_:)-1bt1b.md)
