Home Swift UNIX C Assembly Go Web MCU Research Non-Tech

How Swift parses URLs

2022-07-08 | Swift | #Words: 133 | 中文原版

Although Swift is not a common language for server backends, but it can also parse URLs. Just in case, I still did some research.

Assuming the URL being parsed is: https://www.example.com/?keyword=abc. Code as below:

import UIKit

// Convert the URL string into a URL type constant (variables are fine)
let url = URL(string: "https://www.example.com/?keyword=abc")

// Output the query part (best not use an exclamation mark here to force it to "yes" to prevent errors)
print(url?.query ?? "No query")

Output:

keyword=abc

swift get query of url

I hope these will help someone in need~