---
title: "subarray(with:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsarray/subarray(with:)"
---

# subarray(with:)

Returns a new array containing the receiving array’s elements that fall within the limits specified by a given range.

## Declaration

```swift
func subarray(with range: NSRange) -> [Any]
```

## Parameters

- `range`: A range within the receiving array’s range of elements.

## Return Value

Return Value A new array containing the receiving array’s elements that fall within the limits specified by range.

## Discussion

Discussion If range isn’t within the receiving array’s range of elements, an NSRangeException is raised. For example, the following code example creates an array containing the elements found in the first half of wholeArray (assuming wholeArray exists). NSArray *halfArray; NSRange theRange;   theRange.location = 0; theRange.length = [wholeArray count] / 2;   halfArray = [wholeArray subarrayWithRange:theRange];

## See Also

### Deriving New Arrays

- [adding(_:)](foundation/nsarray/adding(_:).md)
- [addingObjects(from:)](foundation/nsarray/addingobjects(from:).md)
- [filtered(using:)](foundation/nsarray/filtered(using:).md)
