Converting NSURL to NSString in Swift: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to the Task: Why Are NSURL and NSString So Important?

\n\n

Every iOS developer sooner or later faces this task: you have an NSURL object, but you need to get a regular string from it — NSString (or, in modern Swift, just String). It seems simple enough, right? However, there are many pitfalls hidden here: from incorrect character decoding to losing URL fragments.

\n\n

On Stack Overflow, the question "Convert an NSURL to an NSString" has gathered thousands of views — it's a true classic. Here at PythonLib, we decided to cover the topic thoroughly so you never waste time searching for a solution again. Let's go!

\n\n

What Are NSURL and NSString in Swift?

\n\n

Before diving into the code, let's quickly recall what we're dealing with.

\n\n

NSURL is an object representing a Uniform Resource Locator (URL). It can parse addresses into components: scheme, host, path, parameters, and so on. In Swift, it has been replaced by the native URL type, but NSURL is still actively used in legacy code, Core Foundation, and when working with Objective-C libraries.

\n\n

NSString is a string in Objective-C that automatically bridges to the String type in Swift. Therefore, when we say "convert NSURL to NSString," we are actually getting either a String or an NSString?.

\n\n

Main Ways to Convert NSURL to NSString

\n\n

Let's look at three main approaches. Each is suitable for different scenarios.

\n\n

1. Using the absoluteString Property

\n\n

The most popular and simplest method. The absoluteString property returns the full URL string, including all components.

\n\n
import Foundation\n\nlet url = NSURL(string: "https://pythonlib.ru/swift-guide?page=1&lang=ru")!\nlet urlString = url.absoluteString\n\nprint(urlString) // "https://pythonlib.ru/swift-guide?page=1&lang=ru"
\n\n

Important: absoluteString returns a string that may contain percent-encoding. For example, spaces will appear as %20, and Cyrillic characters as %D0%9F.

\n\n

2. The relativeString Property

\n\n

If your URL is relative (does not contain a scheme and host), relativeString will return only the relative part.

\n\n
let baseURL = NSURL(string: "https://pythonlib.ru")!\nlet relativeURL = NSURL(string: "swift-guide", relativeTo: baseURL)!\n\nlet absolute = relativeURL.absoluteString\nlet relative = relativeURL.relativeString\n\nprint(absolute) // "https://pythonlib.ru/swift-guide"\nprint(relative) // "/swift-guide"
\n\n

This method is especially useful when working with base URLs and relative paths.

\n\n

3. Manually Parsing URL Components

\n\n

Sometimes you don't need the full URL, only a part of it: the path, host, or parameters. For this, use the properties of NSURLComponents or directly access NSURL.

\n\n
let url = NSURL(string: "https://pythonlib.ru/ios/guide?query=swift#section1")!\n\nlet scheme = url.scheme ?? ""\nlet host = url.host ?? ""\nlet path = url.path ?? ""\nlet query = url.query ?? ""\nlet fragment = url.fragment ?? ""\n\nprint("Scheme: \\(scheme)")\nprint("Host: \\(host)")\nprint("Path: \\(path)")\nprint("Query: \\(query)")\nprint("Fragment: \\(fragment)")
\n\n

This approach gives you maximum control over what exactly you extract.

\n\n

Common Mistakes and How to Avoid Them

\n\n

Even experienced developers sometimes stumble on simple things. Here are three most frequent problems when converting NSURL to NSString.

\n\n

Mistake 1: Forgetting About Optionals

\n\n

NSURL(string:) returns an optional type NSURL?. If the string

Blogs

Book Recommendations