swift 正则表达式
Expressible literals allow you to initialize types by making use of literals. There are multiple protocols available in the Swift standard library, the chances are you’ve already been using one.
可表达的文字使您可以通过使用文字来初始化类型。 Swift标准库中有多种可用协议,您可能已经使用过一种。
An example is the ExpressibleByStringLiteral
allowing us to initialize a String
using surrounding double quotes instead of using the String(init:)
method. We can all benefit from the fact that Swift is built through these protocols by adopting the protocols in our own defined types.
一个示例是ExpressibleByStringLiteral
使我们可以使用双引号而不是String(init:)
方法来初始化String
。 通过在我们自己定义的类型中采用协议,Swift是通过这些协议构建的,我们都可以从中受益。
什么是文字? (What’s a Literal?)
A literal is a notation for representing a fixed value such as integers, strings, and booleans. Literals in Swift are made possible by several available protocols. Standard types conform to these protocols and allow us to initialize values as follows:
文字是表示固定值(例如整数,字符串和布尔值)的一种表示法。 Swift中的文字可以通过几种可用的协议来实现。 标准类型符合这些协议,并允许我们按以下方式初始化值:
var integer = 0 // ExpressibleByIntegerLiteral
var string = "Hello!" // ExpressibleByStringLiteral
var array = [0, 1, 2] // ExpressibleByArrayLiteral
var dictionary = ["Key": "Value"] // ExpressibleByDictionaryLiteral
var boolean = true // ExpressibleByBooleanLiteral
Those are the most commonly used ones but there are a few others. They’re worth exploring and while you do, you might run into a special protocol called ExpressibleByNilLiteral
.
这些是最常用的,但还有其他一些。 它们值得探索,在您执行操作时,您可能会遇到称为ExpressibleByNilLiteral
的特殊协议。
The ExpressibleByNilLiteral
is used to make it possible to initialize optionals using nil
:
ExpressibleByNilLiteral
用于使用nil
初始化可选选项:
var optional: String? = nil // ExpressibleByNilLiteral
This protocol is used for optionals only and should not be used for custom types. Basically, just ignore this one.
该协议仅用于可选选项,不应用于自定义类型。 基本上,只是忽略这一点。
向自定义类型添加文字支持 (Adding Literal Support to Custom Types)
In most cases, you’ll be fine making use of the default integrated adoption of the protocols. However, it could be that you want to add custom literal support to existing types or to custom types defined in your project.
在大多数情况下,可以很好地利用协议的默认集成采用方式。 但是,可能是您想向现有类型或项目中定义的自定义类型添加自定义文字支持。
To explain how this works we’ll take the example of adding string literal support to URLs. This allows us to initialize a URL directly from a string.
为了解释它是如何工作的,我们将以为URL添加字符串文字支持为例。 这使我们可以直接从字符串初始化URL。
To do this, we need to adopt the ExpressibleByStringLiteral
protocol for the URL
type:
为此,我们需要对URL
类型采用ExpressibleByStringLiteral
协议:
extension URL: ExpressibleByStringLiteral {
public init(stringLiteral value: StaticString) {
self.init(string: "\(value)")!
}
}
It’s worth pointing out the limitations here as we can’t create a failable or throwing initializer. This requires us to force unwrap the created URL which might not be what you want. If that’s the case, you can always fall back to using the URL(string:)
initializer directly.
值得指出此处的局限性,因为我们无法创建可失败的或引发初始化的初始化器。 这就要求我们强制解开可能不是您想要的URL。 如果是这种情况,您总是可以直接使用URL(string:)
初始化程序。
If for your purposes it does make sense to force unwrap, you can start using the literal as follows:
如果出于您的目的强制展开确实有意义,则可以按如下方式开始使用文字:
let url: URL = "https://www.avanderlee.com"
This can simplify the creation of URLs, especially if you’re sure that the URL is valid. Note that we’re making use of a StaticString
type so dynamic strings aren't supported. Dynamic strings would add extra complexity, like URL encoding parameters. You could support this but it's not the focus of this blog post.
这可以简化URL的创建,尤其是在您确定URL有效的情况下。 请注意,我们使用的是StaticString
类型,因此不支持动态字符串。 动态字符串会增加额外的复杂性,例如URL编码参数。 您可以支持这一点,但这不是本博客文章的重点。
不同类型的琴弦 (Different Types of Strings)
When you add support for string literals you should consider adopting the ExpressibleByUnicodeScalarLiteral
for Unicode scalar support and ExpressibleByExtendedGraphemeClusterLiteral
for supporting extended grapheme clusters. An extended grapheme cluster is a group of one or more Unicode scalar values that become a single visible character.
添加对字符串文字的支持时,应考虑采用ExpressibleByUnicodeScalarLiteral
来支持Unicode标量,而ExpressibleByExtendedGraphemeClusterLiteral
来支持扩展字素簇。 扩展字素簇是一组一个或多个Unicode标量值,它们变成一个可见字符。
If you want to support all three cases at once you could add the following extensions:
如果要一次支持所有这三种情况,则可以添加以下扩展:
在自定义类型中使用文字的有用示例 (Useful Examples of Using Literals in Custom Types)
You can make use of literals in your custom types. Here are a few examples to inspire you.
您可以在自定义类型中使用文字。 以下是一些启发您的示例。
直接从字符串初始化日期 (Initializing dates directly from a string)
When you often using the same date format you can initialize your dates from a String
directly:
当您经常使用相同的日期格式时,可以直接从String
初始化日期:
从字典初始化自定义缓存 (Initializing a custom cache from a dictionary)
If you defined your own custom caching store you might want to initialize it directly from a dictionary:
如果定义了自己的自定义缓存存储,则可能需要直接从字典中对其进行初始化:
初始化自定义自动布局优先级 (Initializing custom auto-layout priorities)
When you’re writing auto-layout in code you might need to set a custom layout priority:
Note: We can’t make use of the ExpressibleByFloatLiteral
protocol as the input value will always be seen as an integer. Therefore, we have to use the ExpressibleByIntegerLiteral
protocol.
注意:我们不能使用ExpressibleByFloatLiteral
协议,因为输入值将始终被视为整数。 因此,我们必须使用ExpressibleByIntegerLiteral
协议。
结论 (Conclusion)
That’s it! Hopefully, you’re been inspired by what you can do with literals in Swift. Perhaps you have your own custom types that go well with the available literal protocols.
而已! 希望您能从Swift中的文字处理中受益匪浅。 也许您有自己的自定义类型,可以与可用的文字协议很好地配合使用。
If you would like to improve your Swift knowledge even more, 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/explaining-expressible-literals-in-swift-e7a55514edbd
swift 正则表达式