Introduction to the Task: Why Are NSURL and NSString So Important?
\n\nEvery 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.
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\nWhat Are NSURL and NSString in Swift?
\n\nBefore diving into the code, let's quickly recall what we're dealing with.
\n\nNSURL 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.
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?.
Main Ways to Convert NSURL to NSString
\n\nLet's look at three main approaches. Each is suitable for different scenarios.
\n\n1. Using the absoluteString Property
\n\nThe most popular and simplest method. The absoluteString property returns the full URL string, including all components.
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\nImportant: absoluteString returns a string that may contain percent-encoding. For example, spaces will appear as %20, and Cyrillic characters as %D0%9F.
2. The relativeString Property
\n\nIf your URL is relative (does not contain a scheme and host), relativeString will return only the relative part.
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\nThis method is especially useful when working with base URLs and relative paths.
\n\n3. Manually Parsing URL Components
\n\nSometimes 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.
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\nThis approach gives you maximum control over what exactly you extract.
\n\nCommon Mistakes and How to Avoid Them
\n\nEven experienced developers sometimes stumble on simple things. Here are three most frequent problems when converting NSURL to NSString.
Mistake 1: Forgetting About Optionals
\n\nNSURL(string:) returns an optional type NSURL?. If the string