Skip to content

Commit 8fa365d

Browse files
committed
refactor: adjust logic and syntax avoid force unwrap
1 parent c0aae8f commit 8fa365d

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

xcode/Shared/UrlPolyfill.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,18 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin
5252
guard let scheme = url.scheme else {
5353
return nil
5454
}
55+
var port = ""
56+
if let portInt = url.port { port = String(portInt) }
57+
if (scheme == "http" && port == "80") { port = "" }
58+
if (scheme == "https" && port == "443") { port = "" }
5559
/*
5660
The issue still exists as of macOS 14.4, iOS 17.0
5761
https://stackoverflow.com/questions/74445926/url-host-deprecated-but-replacement-crashes
5862
https://forums.swift.org/t/does-url-query-percentencoded-calls-url-host-percentencoded-under-the-hood/70452
5963
https://forums.developer.apple.com/forums/thread/722451
6064
*/
61-
guard let hostname = url.host else {
62-
return nil
63-
}
64-
var port = (url.port == nil) ? "" : String(url.port!)
65-
if (scheme == "http" && port == "80") { port = "" }
66-
if (scheme == "https" && port == "443") { port = "" }
6765
if #available(macOS 15.0, iOS 18.0, *) {
68-
// let hostname = url.host(percentEncoded: true) ?? ""
66+
guard let hostname = url.host(percentEncoded: true) else { return nil }
6967
let host = (port == "") ? hostname : "\(hostname):\(port)"
7068
let query = url.query(percentEncoded: true) ?? ""
7169
let fragment = url.fragment(percentEncoded: true) ?? ""
@@ -83,6 +81,7 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin
8381
"username": url.user(percentEncoded: true) ?? ""
8482
]
8583
} else {
84+
guard let hostname = url.host else { return nil }
8685
let host = (port == "") ? hostname : "\(hostname):\(port)"
8786
let query = url.query ?? ""
8887
let fragment = url.fragment ?? ""

0 commit comments

Comments
 (0)