I'm experiencing an issue with the Foundation struct URL's string initializer. I'll post some code from the repl below:
Welcome to Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7).
Type :help for assistance.
1> import Foundation
2> let testString = "https://www.apple.com"
testString: String = "https://www.apple.com"
3> let testUrl1 = URL(string: testString)
testUrl1: URL? = nil
4> let testUrl2 = URL(string: "https://www.apple.com")
testUrl2: URL? = nil
I cannot think of why this is happening, if you look at the source for the initializer, located at https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/URL.swift#L495 you'll see this in the documentation:
/// Initialize with string.
///
/// Returns `nil` if a `URL` cannot be formed with the string (for example, if the string contains characters that are illegal in a URL, or is an empty string).
As far as I can tell though, the string I am testing with is a valid URL and the initializer shouldn't be returning nil. I have tried this on two different Macs and in a freshly installed virtual machine, and have gotten the same result in all of them. macOS 10.15, Xcode 11.1. Anyone have any insight into what might be wrong?
It would appear to be a REPL issue and existed in 11.0, too. But if you print(testUrl1), you’ll see it really is set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With