<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSString/pathComponents",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(py)pathComponents"
  },
  "title" : "pathComponents"
}
-->

# pathComponents

The file-system path components of the receiver.

```
var pathComponents: [String] { get }
```

## Discussion

The strings in the array appear in the order they did in the receiver. If the string begins or ends with the path separator, then the first or last component, respectively, will contain the separator. Empty components (caused by consecutive path separators) are deleted. For example, this code excerpt:

```objc
NSString *path = @"tmp/scratch";
NSArray *pathComponents = [path pathComponents];
```

produces an array with these contents:

|Index|Path Component|
|-----|--------------|
|0    |“`tmp`”       |
|1    |“`scratch`”   |

If the receiver begins with a slash—for example, “`/tmp/scratch`”—the array has these contents:

|Index|Path Component|
|-----|--------------|
|0    |“`/`”         |
|1    |“`tmp`”       |
|2    |“`scratch`”   |

If the receiver has no separators—for example, “`scratch`”—the array contains the string itself, in this case “`scratch`”.

Note that this method only works with file paths—not, for example, string representations of URLs.

## See Also

[`-  componentsSeparatedByString:`](/documentation/Foundation/NSString/components(separatedBy:)-238fy)

Returns an array containing substrings from the receiver that have been divided by a given separator.



---

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)