1

I've seen the Q/A here about the self keyword in Swift.

However, to me this doesn't explain the use of .self in the following code fragment from this question

let attributes: [String: Any] =
    [kSecAttrKeyType as String:CFString.self,
     kSecAttrKeySizeInBits as String:numberOfBits]

What does self do in this case?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263

1 Answers1

3

Type.self is the type as a value.

There is a difference between

let s : String = "hello world"

where String is declaring the type of s, and

let sometype : Any = String.self

where we are assigning the String type itself as the value to be stored. That's called the metatype.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And of course there are always the docs: https://docs.swift.org/swift-book/ReferenceManual/Types.html#ID455 – matt Jun 08 '18 at 01:35
  • 1
    Am I correct to assume these are considered "[Postfix Self Expressions](https://docs.swift.org/swift-book/ReferenceManual/Expressions.html#ID401)"? – l'L'l Jun 08 '18 at 02:17