To parse a URL in Swift, you can use the URLComponents
API from the Foundation framework. Here's an example:
main.swift509 chars17 lines
In this example, we first create a string that represents a URL. Then we create an instance of URL
from this string. We then use optional binding to extract the individual components of the URL such as the scheme, host, path, and query items.
Note that the query items are obtained by using the URLComponents
API, which is a more effective way of dealing with the URL query parameters than manually parsing the query string.
Output:
main.swift88 chars6 lines
gistlibby LogSnag