<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "System",
  "identifier" : "/documentation/System/FilePath/extension",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "System"
    ],
    "preciseIdentifier" : "s:6System8FilePathV9extensionSSSgvp"
  },
  "title" : "extension"
}
-->

# extension

The extension of the file or directory last component.

```
var `extension`: String? { get set }
```

## Discussion

If `lastComponent` is `nil` or one of the special path components
`.` or `..`, `get` returns `nil` and `set` does nothing.

If `lastComponent` does not contain a `.` anywhere, or only
at the start, `get` returns `nil` and `set` will append a
`.` and `newValue` to `lastComponent`.

Otherwise `get` returns everything after the last `.` and `set` will
replace the extension.

Examples:

- `/tmp/foo.txt                  => txt`
- `/Applications/Foo.app/        => app`
- `/Applications/Foo.app/bar.txt => txt`
- `/tmp/foo.tar.gz               => gz`
- `/tmp/.hidden                  => nil`
- `/tmp/.hidden.                 => ""`
- `/tmp/..                       => nil`

Example:

```
var path = "/tmp/file"
path.extension = "txt" // path is "/tmp/file.txt"
path.extension = "o"   // path is "/tmp/file.o"
path.extension = nil    // path is "/tmp/file"
path.extension = ""     // path is "/tmp/file."
```

---

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)