From 8fa365d3b90d2de152e91576e2ba8df65d36c88f Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sat, 21 Sep 2024 11:31:21 +0800 Subject: [PATCH] refactor: adjust logic and syntax avoid force unwrap --- xcode/Shared/UrlPolyfill.swift | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/xcode/Shared/UrlPolyfill.swift b/xcode/Shared/UrlPolyfill.swift index 45e090ad..4f8d4180 100644 --- a/xcode/Shared/UrlPolyfill.swift +++ b/xcode/Shared/UrlPolyfill.swift @@ -52,20 +52,18 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin guard let scheme = url.scheme else { return nil } + var port = "" + if let portInt = url.port { port = String(portInt) } + if (scheme == "http" && port == "80") { port = "" } + if (scheme == "https" && port == "443") { port = "" } /* The issue still exists as of macOS 14.4, iOS 17.0 https://stackoverflow.com/questions/74445926/url-host-deprecated-but-replacement-crashes https://forums.swift.org/t/does-url-query-percentencoded-calls-url-host-percentencoded-under-the-hood/70452 https://forums.developer.apple.com/forums/thread/722451 */ - guard let hostname = url.host else { - return nil - } - var port = (url.port == nil) ? "" : String(url.port!) - if (scheme == "http" && port == "80") { port = "" } - if (scheme == "https" && port == "443") { port = "" } if #available(macOS 15.0, iOS 18.0, *) { -// let hostname = url.host(percentEncoded: true) ?? "" + guard let hostname = url.host(percentEncoded: true) else { return nil } let host = (port == "") ? hostname : "\(hostname):\(port)" let query = url.query(percentEncoded: true) ?? "" let fragment = url.fragment(percentEncoded: true) ?? "" @@ -83,6 +81,7 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin "username": url.user(percentEncoded: true) ?? "" ] } else { + guard let hostname = url.host else { return nil } let host = (port == "") ? hostname : "\(hostname):\(port)" let query = url.query ?? "" let fragment = url.fragment ?? ""