I'm trying to parse URLs that may not have a scheme specified. In that case, I want it to assume http, so I set the scheme strategy to .defaultValue("http"). However, when parsing a URL without a scheme, it fails with the following error:
error: Error Domain=NSCocoaErrorDomain Code=2048 "Cannot parse example.com. String should adhere to the preferred format of the locale, such as https://user:password@www.example.com/path?color=red#name."If I prefix the URL string with "http://", it works fine.
I also tried setting the default value to "http:" and "http://" with the same result.
Here is the code I'm using to test this:
import Foundation
do {
let url = try URL(
"example.com", // this fails but "http://example.com" works
strategy: .url
.scheme(.defaultValue("http"))
.port(.defaultValue(8080))
.path(.defaultValue("/"))
)
print("parsed: \(url)")
} catch let error {
print("error: \(error)")
}Is this a bug, or have I misunderstood the default value strategy?
Thanks!
0
0
75