pathComponents
The file-system path components of the receiver.
Declaration
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:
NSString *path = @"tmp/scratch";
NSArray *pathComponents = [path pathComponents];produces an array with these contents:
Index | Path Component |
|---|---|
0 | “ |
1 | “ |
If the receiver begins with a slash—for example, “/tmp/scratch”—the array has these contents:
Index | Path Component |
|---|---|
0 | “ |
1 | “ |
2 | “ |
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
Related Documentation
Working with Paths
path(withComponents:)completePath(into:caseSensitive:matchesInto:filterTypes:)fileSystemRepresentationgetFileSystemRepresentation(_:maxLength:)isAbsolutePathlastPathComponentpathExtensionabbreviatingWithTildeInPathappendingPathComponent(_:)appendingPathExtension(_:)deletingLastPathComponentdeletingPathExtensionexpandingTildeInPathresolvingSymlinksInPathstandardizingPath