Converting NSURL to NSString in Swift: How to get a string from a URL?
Hi everyone! I'm new to Swift and ran into a problem working with URLs. I have a `URL` (or `NSURL`) object, obtained, for example, from the `URL(string:)` method or from a server response. I need to output this URL as a regular string to show it to the user or save it to a log. But when I try to simply use `print(url)`, I get something like "file:///Users/me/Documents/file.txt" or "https://example.com", but I need just "https://example.com" or "/Users/me/Documents/file.txt". How do I correctly convert `NSURL` to `NSString` or `String` in Swift? I've tried different ways, but the compiler complains.
Here's an example of my code:
```swift
import Foundation
let urlString = "https://www.example.com/path/to/file.txt"
if let url = URL(string: urlString) {
// How to get
No answers yet.
Log in to leave an answer