<!--
{
  "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/appendingPathExtension(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSString(im)stringByAppendingPathExtension:"
  },
  "title" : "appendingPathExtension(_:)"
}
-->

# appendingPathExtension(_:)

Returns a new string made by appending to the receiver an extension separator followed by a given extension.

```
func appendingPathExtension(_ str: String) -> String?
```

## Parameters

`str`

The extension to append to the receiver.

## Return Value

A new string made by appending to the receiver an extension separator followed by `ext`.

## Discussion

The following table illustrates the effect of this method on a variety of different paths, assuming that `ext` is supplied as `@"tiff"`:

|Receiver’s String Value|Resulting String         |
|-----------------------|-------------------------|
|“`/tmp/scratch.old`”   |“`/tmp/scratch.old.tiff`”|
|“`/tmp/scratch.`”      |“`/tmp/scratch..tiff`”   |
|“`/tmp/`”              |“`/tmp.tiff`”            |
|“`scratch`”            |“`scratch.tiff`”         |

Note that adding an extension to `@"/tmp/"` causes the result to be `@"/tmp.tiff"` instead of `@"/tmp/.tiff"`. This difference is because a file named `@".tiff"` is not considered to have an extension, so the string is appended to the last nonempty path component.

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

### Special Considerations

Prior to OS X v10.9 this method did not allow you to append file extensions to filenames starting with the tilde character (`~`).

---

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)