Swift:反斜杠点"\"的作用是什么.意思是? [英] Swift: What does backslash dot "\." mean?

查看:1565
本文介绍了Swift:反斜杠点"\"的作用是什么.意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是后端Swift的新手,以为我会使用Vapor来快速启动和运行一个副项目... 我运行了vapor new WebServer --template=auth-template,现在我正在尝试弄清return \.email的含义. 有关更多上下文,我在WebServer>源> App>模型> Users.swift中查找:

I'm new to backend Swift and thought I'd use Vapor to get up-and-running on a side project fast... I ran vapor new WebServer --template=auth-template, and now I'm trying to figure out what something like return \.email means. For more context, I'm looking in WebServer > Sources > App > Models > Users.swift:

import Authentication
import FluentSQLite
import Vapor

/// Allows users to be verified by basic / password auth middleware.
extension User: PasswordAuthenticatable {
    /// See `PasswordAuthenticatable`.
    static var usernameKey: WritableKeyPath<User, String> {
        return \.email
    }

// ...
}

这是User类的定义:

And here is the definition of the User class:

/// A registered user, capable of owning todo items.
final class User: SQLiteModel {
    // {omit extra code} ...

    var email: String

    // {omit extra code} ...

    /// Creates a new `User`.
    init(id: Int? = nil, name: String, email: String, passwordHash: String) {
        // {omit extra code} ...
        self.email = email
        // {omit extra code} ...
    }
}

这种反斜杠符号是什么意思?

What does this backslash-dot notation mean?

推荐答案

tl; dr:被称为 key-path-expression .

(至此,问题已得到充分回答.)

(The question has been sufficiently answered, by this point.)

有关如何获取该埋藏文档的更多动手方法:

从您发布的代码中可以看到,User类包含一个属性称为email.

As you can see from the code you posted, the User class contains a property named email.

请注意,假设您使用的是Xcode,如果将return \.email替换为return \,则会出现编译错误"Expected expression path in Swift key path",因此这暗示此反斜杠点表示法可能与称为关键路径的东西.

Notice that, assuming you're using Xcode, if you replace return \.email with return \, you get the compile-error "Expected expression path in Swift key path", so this is a hint that this backslash-dot notation might have to do with something called a key path.

从有关键路径的文档中,我们看到我们也可以编写\User.email(并且您可以在Xcode中尝试它,而没有编译器错误).

From that documentation on key-path, we see that we could also have written \User.email (and you can try it out in Xcode with no compiler error).

了解该代码中发生的事情的更大上下文:

因此,从语义上来说,要了解您正在查看的usernameKey声明的含义,我们可能想了解WritableKeyPath是什么.简而言之,从文档中,我们看到WritableKeyPath是:支持读取和写入结果值的键路径."

So, semantically, to understand the meaning of the usernameKey declaration you're looking at, we might want to understand what a WritableKeyPath is. In simple, from the documentation, we see that a WritableKeyPath is: "A key path that supports reading from and writing to the resulting value."

因此,我们看到usernameKey声明接受了WritableKeyPath对象,并返回了String,即User.email.

So, we see that the usernameKey declaration takes in a WritableKeyPath object and returns a String that is User.email.

此外,很明显,User类需要此usernameKey属性才能符合PasswordAuthenticatable协议,该协议是在import Authentication的第一行中导入的(如果您想在那里探索,请查看在依赖关系>身份验证2.0.0>身份验证>基本> BasicAuthenticatable.swift"中.

Furthermore, it's apparent that the User class needs this usernameKey property in order to conform to the PasswordAuthenticatable protocol, which was imported on the first line with import Authentication (if you care to explore there, take a look at Dependencies > Auth 2.0.0 > Authentication > Basic > BasicAuthenticatable.swift).

这篇关于Swift:反斜杠点"\"的作用是什么.意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆