Instance Method

appendingPathExtension(_:)

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

Declaration

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

Parameters

ext

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 (~).

See Also

Related Symbols

func appendingPathComponent(String)

Returns a new string made by appending to the receiver a given string.

var deletingPathExtension: String

A new string made by deleting the extension (if any, and only the last) from the receiver.