3

In this video: https://developer.apple.com/videos/play/wwdc2019/103/, the following snippet of code is shown at around 15:30:

...
ForEach(ContentSizeCategory.common.identified(by: \.self))
...

What does it do? Where does self points to? The current object (TraitCell_Preview)? It doesn't even compile in my computer since common isn't a member in ContentSizeCategory. I thought I had seen it before in a SwiftUI talk (\.self) . Keypath's aren't my best thing in Swift though.

I understand ForEach's elements needs to be Identifiable. self (a.k.a. TraitCell_Preview right?) does only conform to PreviewProvider so not Identifiable (if the private _PreviewProvider protocol from which PreviewProvider conforms to doesn't conform to Identifiable, not sure about that since I can not see the code).

What is \.self in the snippet of code and where does it points to?

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • 1
    Nonsense that this question is marked as a duplicate question when the other question is asked later than this question. The other linked question should be marked as the duplicated question. – J. Doe Jun 07 '19 at 11:02
  • If there was no upvoted or accepted answer at the moment Sulthan maked your question as dupe, they couldn't have done so. More info [here](https://meta.stackexchange.com/questions/187827/should-questions-be-marked-as-duplicate-if-the-older-question-has-no-accepted-an) – Aserre Jun 07 '19 at 13:16

2 Answers2

4
  1. Looks like common is a static variable that they created to help their demo. It's just an extension on ContentSizeCategory. Something like this:
extension ContentSizeCategory {
     static var common = [ContentSizeCategory.accessibilityLarge,
                          ContentSizeCategory.accessibilityMedium,
                        ContentSizeCategory.extraSmall]

}
  1. ContentSizeCategory is an enum, conforms to Hashable that means every type is uniquely identifiable. Below is the signature of identified function, when it is called you need to tell what's the key path that it should use in order to uniquely identify items. So \.self is basically telling the whole self is unique because it conforms to Hashable.
func identified<ID>(by getID: KeyPath<Binding<Value.Element>, ID>) -> IdentifierValuePairs<Binding<Value>, ID> where ID : Hashable
SMP
  • 1,629
  • 7
  • 15
1

Method self is a property that returns the object itself, for example:

let string = "text"
print(string[keyPath: \.self]) // "text"

We also use it when accessing types, e.g. Int.self.

I assume that the entire instance is used as identifier.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • So that means the member common is Identifiable? It's a shame it doesn't compile so I can not have a look myself – J. Doe Jun 05 '19 at 21:43
  • @J.Doe It's an enumeration. That means the values are already unique and you don't have to provide any other unique identifier. However, since we cannot really compile, I wouldn't worry about that now. – Sulthan Jun 05 '19 at 22:32