macbook路径快捷键

    科技2022-08-01  119

    macbook路径快捷键

    Difficulty: Beginner | Easy | Normal | ChallengingThis article has been developed using Xcode 11.4.1, and Swift 5.2.2

    难度:初学者| 容易| 普通 | 具有挑战性本文是使用Xcode 11.4.1和Swift 5.2.2开发的

    先决条件 (Prerequisites)

    You will be expected to be aware how to make a Single View Application, or a Playground to run Swift code

    您应该知道如何制作Single View Application或Playground来运行Swift代码

    术语 (Terminology)

    keypath: Read-only access to a property writablekeypath: read-write access to a value-type property referencewritablekeypath: read-write access to a reference-type property

    keypath:对属性的只读访问权限writablekeypath:对值类型的属性引用进行读写访问writablekeypath:对引用类型的属性进行读写访问

    动机 (Motivation)

    Combine uses ReferenceWritableKeyPath<Root, Bool> in the assign function

    Combine在分配函数中使用ReferenceWritableKeyPath<Root, Bool>

    which is the one that allows us to connect to UIKit elements, for example

    例如,它允许我们连接到UIKit元素

    That seems fine, but what are keypaths? How are they used, and what do they mean?

    看起来不错,但是关键路径是什么? 它们是如何使用的,它们是什么意思?

    Could you make an article explaining this for me?

    您能写一篇文章为我解释一下吗?

    I don’t mind if I do!

    我不介意我这样做!

    什么是关键路径? (What is a keypath?)

    A keypath provides read-only access to a property, whilst a writable keypath provides (well…) writable access to a property.

    键路径提供对属性的只读访问权限,而可写键路径提供(对……)可写属性访问权限。

    关键路径示例 (A keypath example)

    Perhaps the best way to describe this access is through an example, where we can set up a rather basic struct object.

    描述此访问权限的最佳方法可能是通过示例,在该示例中我们可以设置一个相当基本的struct对象。

    struct Person { var firstname: String var secondname: String var age: Int } let dave = Person(firstname: "Dave", secondname: "Trencher" , age: 21)

    we can then access the properties through WritableKeyPath<Person, String> or WritableKeyPath<Person, Int> (where firstname and secondname are represented by a String and age is represeted by a Int).

    然后,我们可以通过WritableKeyPath<Person, String>或WritableKeyPath<Person, Int> (其中firstname和secondname由String表示,age由Int预置)来访问属性。

    The following keypath therefore returns a String, and that can be printed out

    因此,以下键路径返回String ,并且可以将其打印出来

    let firstname: String = dave[keyPath: \Person.firstname] print (firstname) // Dave

    Now even the property type can be stored

    现在甚至可以存储属性类型

    var writableKeyPathFirstName: WritableKeyPath<Person, String> = \Person.firstname print (dave[keyPath: writableKeyPathFirstName]) // Dave

    which means that you can potentially use the same property in multiple places, and storing it as a property itself means that it would only need to be stored in one place.

    这意味着您可能会在多个位置使用相同的属性,并将其存储为属性本身意味着只需要将其存储在一个位置即可。

    嵌套属性 (Nested properties)

    The following is an example of nested keypaths

    以下是嵌套键路径的示例

    struct Socks { var sockname: String } struct DrawContents { var name: String var socks: Socks } let topdrawer = DrawContents(name: "top", socks: Socks(sockname: "Birthday Socks")) print (topdrawer) print (topdrawer[keyPath: \DrawContents.name]) // top print (topdrawer[keyPath: \DrawContents.socks.sockname]) // Birthday Socks

    KeyPath组成 (KeyPath composition)

    Swift allows you to dynamically combine keypaths at runtime (of course the types need to be compatible).

    Swift允许您在运行时动态组合键路径(当然,类型必须兼容)。

    let topdrawerkpath = \DrawContents.socks let sockspath = \Socks.sockname let composedPath: WritableKeyPath<DrawContents, String> = topdrawerkpath.appending(path: sockspath)

    KeyPaths作为类型擦除的变体 (KeyPaths as type-erased variants)

    You may wish to have a keypath that does not require the Value parameter

    您可能希望使用不需要Value参数的键路径

    let drawerName: PartialKeyPath<DrawContents> = \.name let drawerSocks: PartialKeyPath<DrawContents> = \.socks

    So both drawerName and drawerSocks can be stored with the same type: PartialKeyPath<DrawContents>. The Value parameter has been type-erased.

    因此, drawerName和drawerSocks都可以使用相同的类型存储: PartialKeyPath<DrawContents> 。 Value参数已被类型擦除。

    引用类型的关键路径 (KeyPaths of reference types)

    This shouldn’t be a too big surprise, but if you use a keypath on a reference type (for example a class )

    这应该不是一个太大的惊喜,但是如果您在引用类型(例如class )上使用键路径,

    let horse = Animal(name: "Keith") var refKeyPath: ReferenceWritableKeyPath<Animal, String> = \Animal.name let animalname: String = horse[keyPath: \Animal.name] // Keith

    密钥路径是新的吗? (Are keypaths new?)

    Keypaths have been around for some time, they’re present in Objective-C! However, they were not type safe ( keyPath() is actually a String).

    关键路径已经存在了一段时间,它们存在于Objective-C ! 但是,它们不是类型安全的( keyPath()实际上是String )。

    #keyPath(Person.firstname)

    局限性 (Limitations)

    Key path cannot refer to static member 'lifeform'

    Key path cannot refer to static member 'lifeform'

    If we change Person to have a static var.

    如果我们将Person更改为具有静态变量。

    struct Person { static var lifeform = "Carbon" var firstname: String var secondname: String var age: Int } dave[keyPath: \Person.lifeform] // Key path cannot refer to static member 'lifeform'

    So, keypaths are actully useful in iOS development, being relevant for Combine and SwiftUI. You want to buckle up and become familiar with this, or not? I'd say that the former is more important - and you can read up on this HERE.

    因此,键路径在iOS开发中确实有用,与Combine和SwiftUI有关。 您想系好安全带并熟悉它吗? 我想说前者更重要-您可以在这里阅读。

    I hope that this article has helped you out in become more familar in this relatively new feature of Swift.

    我希望本文能帮助您熟悉Swift的这一相对较新的功能。

    If you’ve any questions, comments or suggestions please hit me up on Twitter

    如果您有任何疑问,意见或建议,请在Twitter上打我

    翻译自: https://medium.com/@stevenpcurtis.sc/what-are-swifts-keypaths-e8c829bc97d3

    macbook路径快捷键

    Processed: 0.010, SQL: 8