---
title: "attribute(_:at:longestEffectiveRange:in:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsattributedstring/attribute(_:at:longesteffectiverange:in:)"
---

# attribute(_:at:longestEffectiveRange:in:)

Returns the value for the attribute with the specified name of the character at the specified index and, by reference, the range where the attribute applies.

## Declaration

```swift
func attribute(_ attrName: NSAttributedString.Key, at location: Int, longestEffectiveRange range: NSRangePointer?, in rangeLimit: NSRange) -> Any?
```

## Parameters

- `attrName`: The name of an attribute.
- `location`: The index at which to test for attributeName.
- `range`: If non-NULL: If the named attribute exists at index, upon return aRange contains the full range over which the value of the named attribute is the same as that at index, clipped to rangeLimit. If the named attribute does not exist at index, upon return aRange contains the full range over which the attribute does not exist, clipped to rangeLimit. If you don’t need this value, pass NULL.
- `rangeLimit`: The range over which to search for continuous presence of attributeName. This value must not exceed the bounds of the receiver.

## Return Value

Return Value The value for the attribute named attributeName of the character at index, or nil if there is no such attribute.

## Discussion

Discussion Raises an rangeException if index or any part of rangeLimit lies beyond the end of the receiver’s characters. If you don’t need the longest effective range, it’s far more efficient to use the attribute(_:at:effectiveRange:) method to retrieve the attribute value. For a list of possible attributes, see NSAttributedString.Key.

## See Also

### Getting attributes for a range of text

- [attributes(at:effectiveRange:)](foundation/nsattributedstring/attributes(at:effectiverange:).md)
- [attributes(at:longestEffectiveRange:in:)](foundation/nsattributedstring/attributes(at:longesteffectiverange:in:).md)
- [attribute(_:at:effectiveRange:)](foundation/nsattributedstring/attribute(_:at:effectiverange:).md)
- [enumerateAttribute(_:in:options:using:)](foundation/nsattributedstring/enumerateattribute(_:in:options:using:).md)
- [enumerateAttributes(in:options:using:)](foundation/nsattributedstring/enumerateattributes(in:options:using:).md)
- [NSAttributedString.EnumerationOptions](foundation/nsattributedstring/enumerationoptions.md)
