kafka常见应用场景_快速常见场景中的url进行了深入解释

kafka常见应用场景

URLs are everywhere in the apps we build. We reference local files using bundle paths, we fetch data from a path pointing to our API, and we fetch images to display visuals. When working with a URL we often need to verify things, like making sure that it’s pointing to a file or that a certain query property is set.

URL在我们构建的应用程序中无处不在。 我们使用包路径引用本地文件,我们从指向API的路径中获取数据,并获取图像以显示视觉效果。 使用URL时,我们经常需要验证事物,例如确保指向文件或设置了特定的查询属性。

Some of those operations may be easy to remember and don’t require an intensive guide like this. However, I find myself often searching for that one stack overflow question that explains how to build a URL with a given set of query parameters. As a blogger, this is the moment I decide to write a manual for myself and share it with you!

其中一些操作可能很容易记住,并且不需要像这样的详尽指南。 但是,我发现自己经常在搜索一个堆栈溢出问题,该问题解释了如何使用给定的一组查询参数来构建URL。 作为博客作者,这是我决定为自己编写手册并与您共享的时刻!

基本的URL操作 (Basic URL Operations)

It all starts with basic operations to create and work with links. For example, you can convert a string value into a URL as follows:

这一切都始于创建和使用链接的基本操作。 例如,您可以将字符串值转换为URL,如下所示:

let url = URL(string: "https://www.avanderlee.com")!

The downside of this default initializer is the force unwrap we probably have to copy over throughout our codebase. We can help prevent this with a simple extension: a custom initializer:

此默认初始化程序的缺点是强制拆包,我们可能必须在整个代码库中进行复制。 我们可以通过一个简单的扩展来防止这种情况:自定义初始化程序:

extension URL {
init(_ string: String) {
self.init(string: "\(string)")!
}
}
var unwrappedURL = URL("https://www.avanderlee.com")

That looks a lot more Swift and allows us to keep our implementation code clean from unwraps!

这看起来要快得多,而且可以让我们保持实现代码的干净整洁!

将URL转换为字符串 (Converting a URL into a String)

A URL can also be converted into a String by using the absoluteString property:

也可以使用absoluteString属性将URL转换为String

print(unwrappedURL.absoluteString) // Prints: https://www.avanderlee.com

使URL相对于基本URL (Making a URL Relative to a Base URL)

A scenario that’s often used when building an API in Swift is to construct a link by using a base URL. For example, we could build up a blog post category page as follows:

在Swift中构建API时经常使用的场景是使用基本URL来构建链接。 例如,我们可以建立一个博客文章类别页面,如下所示:

let category = "swift"
let baseURL = URL(string: "https://www.avanderlee.com")!
let blogURL = URL(string: category, relativeTo: baseURL)!
print(blogURL) // Prints: swift -- https://www.avanderlee.com
print(blogURL.absoluteString) // Prints: https://www.avanderlee.com/swift

At the same time, you can get the base for a certain link if the URL itself is not absolute:

同时,如果URL本身不是绝对的,则可以获取某个链接的基础:

let absoluteURL = URL(string: "https://www.avanderlee.com")!
let relativeURL = URL(string: "swift", relativeTo: baseURL)!
print(absoluteURL.baseURL) // Prints: nil
print(relativeURL.baseURL!) // Prints: https://www.avanderlee.com

A base URL is often confused with getting the host of a link that prints out the domain without its scheme. You can get the scheme by using the scheme parameter:

基本URL通常与获取链接的宿主相混淆,该链接可以打印出没有其方案的域。 您可以使用scheme参数获取scheme

let swiftLeeURL = URL(string: "https://www.avanderlee.com")!
print(absoluteURL.host!) // Prints: www.avanderlee.com
print(absoluteURL.scheme!) // Prints: https

使用URL组件 (Working with URL Components)

A link is basically built up out of several components. We’ve seen a few already in the print statements before but there are many more components to cover:

链接基本上是由几个组件组成的。 之前,我们已经在打印语句中看到了一些内容,但还有更多内容需要介绍:

let twitterAvatarURL = URL(string: "https://twitter.com/twannl/photo.png?width=200&height=200")!
print(twitterAvatarURL.path) // Prints: /twannl/photo.png
print(twitterAvatarURL.pathComponents) // Prints: ["/", "twannl", "photo.png"]
print(twitterAvatarURL.pathExtension) // Prints: png
print(twitterAvatarURL.lastPathComponent) // Prints: photo.png

These properties are great for a lot of cases, like getting the file name or extension, but they don’t give us the query items. This is where URLComponents come into their own.

这些属性在很多情况下都很有用,例如获取文件名或扩展名,但是它们不给我们查询项。 这是URLComponents独有的地方。

获取查询参数的值 (Get the value of query parameters)

The above Twitter avatar link contains two query parameters that describe the width and height of the image. This is important information, so we make sure that the image is displayed in the correct ratio. So we need to be able to get those query parameters and their values.

上面的Twitter头像链接包含两个查询参数,它们描述图像的宽度和高度。 这是重要的信息,因此我们确保以正确的比例显示图像。 因此,我们需要能够获取那些查询参数及其值。

We can do this by using a URLComponents instance initialized with the link, which gives us access to a new collection of properties. You can basically see it as a URL parser that gives us access to, for example, the query items:

我们可以使用通过链接初始化的URLComponents实例来实现此目的,该实例使我们可以访问新的属性集合。 您基本上可以将其看作是URL解析器,它使我们可以访问例如查询项:

let components = URLComponents(string: "https://twitter.com/twannl/photo.png?width=200&height=200")!
print(components.query!) // width=200&height=200
print(components.queryItems!) // [width=200, height=200]

Note that the URLComponents structure parses and constructs URLs according to RFC 3986. Its behavior differs subtly from that of the URL structure, which conforms to older RFCs. However, you can easily obtain a URL value based on the contents of a URLComponents value or vice versa.

请注意,URLComponents结构根据 RFC 3986 解析和构造URL 它的行为与URL结构的行为有细微差别,后者符合较早的RFC。 但是,您可以根据URLComponents值的内容轻松获得URL值,反之亦然。

This allows us to directly access the values by going over the query items array:

这使我们可以通过遍历查询项目数组来直接访问值:

However, this is still not the most beautiful piece of code. We use a lot of unwraps and it’s not really readable. We can make use of a custom subscript that allows us to make this code look a lot nicer:

但是,这仍然不是最漂亮的代码。 我们使用了大量的包装纸,而且它的可读性很差。 我们可以使用自定义下标,使我们使此代码看起来更好:

New to custom subscripts? Check out my blog post Custom subscripts in Swift explained with code examples.

自定义下标的新手? 查看我的博客文章 Swift中 自定义下标,并提供代码示例

Yes, there are still a lot of unwraps — I’ll leave it up to you to make this look nicer by using guards for unwrapping — but at least we can access the query items more easily.

是的,仍然有很多可拆封的内容,我将由您自己决定,通过使用防护来进行拆包使外观看起来更好些,但是至少我们可以更轻松地访问查询项。

使用参数构造URL (Constructing a URL with Parameters)

The other method is building a URL with query parameters from a dictionary. This is often used when building an API framework.

另一种方法是使用字典中的查询参数构建URL。 在构建API框架时经常使用它。

For example, if we want to fetch an avatar with a certain width and height, we can build it up like this:

例如,如果我们要获取具有特定宽度和高度的化身,我们可以像这样构建它:

It might be valuable to write an extension here as well, to make it easier to convert a dictionary into an array of query items:

在此处编写扩展名可能也很有价值,以使将字典转换为查询项数组变得更加容易:

使用文件URL (Working with File URLs)

When building apps we often reference local or remote files. You can download a certain image from a remote location, or you can fetch an image from your local bundle.

在构建应用程序时,我们经常引用本地或远程文件。 您可以从远程位置下载特定图像,也可以从本地捆绑软件中获取图像。

It’s good to understand the difference between a remote URL and a local one. Only the latter is considered a file URL when using the isFileURL parameter:

理解远程URL和本地URL之间的区别是很好的。 使用isFileURL参数时,只有后者被视为文件URL:

var remoteFileURL = URL(string: "https://www.twitter.com/avatar.jpg")!
var fileURL = URL(string: "file:///users/antoine/avatar.jpg")!
print(remoteFileURL.isFileURL) // Prints: false
print(fileURL.isFileURL) // Prints: true

This property basically only returns true when the URL scheme is file:.

该属性基本上仅在URL方案为file:时返回true。

从文件路径获取文件扩展名 (Getting the file extensions from a file path)

When working with files you often want to know the file extension to determine the file type. You can do this by using the path extension parameter:

在处理文件时,您通常想知道文件扩展名以确定文件类型。 您可以通过使用路径扩展参数来执行此操作:

print(fileURL.pathExtension) // Prints: jpg

如何在Swift中从文件路径获取文件名 (How to get the filename from a file path in Swift)

To get the file name from a file URL we can use the lastPathComponent and extract the filename by removing the file extension:

要从文件URL获取文件名,我们可以使用lastPathComponent并通过删除文件扩展名来提取文件名:

print(fileURL.deletingPathExtension().lastPathComponent) // Prints: avatar

Isn’t that simple? This is great if you want to display the file name in your UI.

这不是很简单吗? 如果您想在用户界面中显示文件名,那就太好了。

结论 (Conclusion)

URLs in Swift are used in a lot of ways. We fetch data using an API, images to visualize our app, and we often work with local files from our bundle. The Foundation framework allows us to access a lot of URL components easily with default parameters. If we need access to URL components like query items, we can make use of the URLComponents type.

Swift中的URL有很多使用方式。 我们使用API​​来获取数据,使用图像来形象化我们的应用程序,并且我们经常使用捆绑软件中的本地文件。 Foundation框架使我们可以使用默认参数轻松访问许多URL组件。 如果我们需要访问URL组件(例如查询项),则可以使用URLComponents类型。

Do you think that I missed an essential part of working with URLs? Please, let me know!

您是否认为我错过了使用URL的必要部分? 拜托, 让我知道

If you would like to continue to improve your Swift knowledge, check out the Swift category page. Feel free to contact me or tweet to me on Twitter if you have any additional tips or feedback.

如果您想继续提高自己的Swift知识,请查看Swift类别页面 。 如果您有其他任何提示或反馈,请随时与我联系或在Twitter上发给我。

Thanks!

谢谢!

翻译自: https://medium.com/better-programming/urls-in-swift-common-scenarios-explained-in-depth-316d606df6b2

kafka常见应用场景

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值