Contents

appendingPathComponent(_:isDirectory:)

Returns a URL by appending the specified path component to self, specifying whether the resulting path is a directory.

Declaration

func appendingPathComponent(_ pathComponent: String, isDirectory: Bool) -> URL

Parameters

  • pathComponent:

    The path component to add.

  • isDirectory:

    If true, the method treats the path component as a directory.

Return Value

A new URL with the path component appended.

Discussion

The URL syntax for a directory and for a file at otherwise the same location are slightly different — directory URLs must end in /. If you append the path component second to the URL http://www.example.com/first/, if isDirectory is true, the resulting URL is http://www.example.com/first/second/. If isDirectory is false, the resulting URL is http://www.example.com/first/second.

This difference is particularly important if you resolve another URL against this new URL. For example, the path component file.html relative to http://www.example.com/first/second is http://www.apple.com/first/file.html, whereas relative to http://www.example.com/first/second/, it’s http://www.example.com/first/second/file.html.

New code should use appending(path:directoryHint:) instead of this method.

See Also

Adding path components