Updated at: 01-06-2023 - By: Craig Huey

Receive Your Free a replica of The Missing Manualfor Swift Development

The Manual I Needed at the Outset

You may feel as if at times Foundation framework that hasn’t been updated in years, but you’ll need to use to build apps for iOS, tvOS, macOS, or watchOS. Nonetheless, this is far from the truth. Year after year, Apple updates the framework and adds new features for programmers. An approach I particularly enjoy using is the URLComponents structure, which I hope to discuss in this episode.

First Things First

Open Xcode and make a new playground by selecting theBlankpattern based on theiOS section.

*

Give the playground a title, then tell Xcode where you want to keep it.

*

It’s time to empty the playground and add an import statement for theFoundation framework. It is important to keep in mind that URLComponents is defined inFoundation.

value of importation Foundation

URLs, Parameters, and Fragments

Constructing a URL requires little effort. Check out this illustration. To get a possible URL instance, use the init?(string:) initializer.

URL(string) = “https://myapi.com”; var url = “” Including a path in the URL is also a viable option.

link = link?.appendingPathComponent(“users”) However, things become complicated when query parameters are included. A good example is ensuring that the query parameters’ keys and values are correctly percent encoded. And how do you modify the URL to include a fragment?

These aren’t insurmountable problems, but you might be wondering why the platform hasn’t made it simpler to work with URLs by now.

Introducing URLComponents

Apple introduced a new font few years ago Foundation framework for working with URLs that is both simple and elegant; examples include Swift’s URLComponents structure and Objective-C’s NSURLComponents class. Both types can be found on devices running iOS 7 or later, Apple TV 9 or later, macOS 10.9 or later, and Apple Watch 2 or later.

Focusing on URLComponents architecture for the rest of this episode. There are two main responsibilities of the URLComponents framework: parsing The use of uniform resource locators (URLs) and constructing URLs. The URLComponents structure requires that URLs adhere to the widely adopted standard RFC 3986.

Allow me to demonstrate how simple it is to build a URL instance using the URLComponents structure. To begin, a new instance of the URLComponents structure must be made.

variables components = URLComponents() After that, we configure the instance’s scheme and host.

Look here:An Evaluation of the Strength of Each Monster Legendary’s Team in a Battle

the components.scheme = “https” and the components.host = “myapi.com” A URL instance may be obtained as a result of querying the URLComponents instance’s url property. That value is identical to “https://myapi.com” right now.

components.url Setting the queryItems property of the URLComponents instance allows us to continue customizing it. The type of this property is. Another minimal but useful modification is the URLQueryItem structure, which Foundation framework.

For example, we could write: let queryItemToken = URLQueryItem(name: “token”, value: “12345”) components.queryItemQuery = URLQueryItem(name: “query”, value: “swift ios”). The URL property of the URLComponents instance is set to https://myapi.com?query=swift ios&token=12345 thanks to the queryItems = operator. Take note that all parameter values are percent encoded by default.

Additionally, authentication and fragment support have been added to the URLComponents framework. Because of this, the address will look like this: https://bartjacobs:mypassword
myapi.com?token=123456&query=swift ios#five.

parts.fragment = “five”parts. To whomever uses the identifier “bartjacobs,” MyPassword = components.password There are many more shortcut methods for making URLs in the URLComponents structure. Information can be retrieved from URLs using this method as well. Allow me to demonstrate how this operates.

Dissecting URLs With URLComponents

The URLComponents structure is excellent for both constructing and inspecting URIs. Examine the case in point that I’ll give you. We get a non-null URLComponents instance if the init?(url:resolvingAgainstBaseURL:) initializer fails.

To do this, we can set url to URL(string: ” https://bartjacobs:mypassword
myapi.com?token=12345&query=swift ios#five”)! You can get the individual parts of a URL by using the formula: let components = URLComponents(url: url, resolvingAgainstBaseURL: false). By calling init?(url:resolvingAgainstBaseURL:), we create a new instance of the URLComponents class. Instances of URLs and boolean values are the two types of parameters defined by this initializer.

Based on the boolean value, the URL instance is evaluated true or false. The second parameter must equal true if a relative URL is passed to the initializer; otherwise, the value of the absoluteURL property will be used instead.

After initializing a new instance of URLComponents, we can now retrieve the individual parts of the URL.

If we assume that parts equal parts, then In order to query components, you can use the notation components.host components.query. percentEncodedQuery To do this, we can use the following code: if let queryItems = components.queryItems then for queryItem in queryItems then print(“(queryItem.name): (queryItem.value)”)

Strings Work Just as Fine

The init?(string:) initializer can be used to create a URLComponents instance from a string. If the string doesn’t follow RFC 3986 guidelines, the initialization will fail.

In other words: Xem thêm:All I Can Do In A Hurry The Best I Can Do is Sing the Lyrics

The string “https://lisbdnet.com” is broken down into its component parts using the formula: components = URLComponents

It’s in the Small Things

Foundation is a robust library that no Cocoa programmer should be without. Despite some feature gaps, Apple keeps working to close them. There is no doubt that the inclusion of the DateInterval structure has greatly improved the Foundation framework a bit more fun to work with. In a later post this week, I’ll demonstrate how convenient it is to use the DateInterval data structure when dealing with time spans of varying lengths.

Receive Your Free Original: Copy of The Missing Manualfor Swift Development

Exactly What I Needed When I First Started Out