DAY #73

What are Swift Metatypes (Part 1)

A metatype is a type of a type. OK post done…..jk

Metatypes are used to access properties and methods (including initializers) belonging to a type - in contrast to an instance. Every time you use a static property or function, you are using a Metatype.

1
2
let whatsGoingOnHere: Int.Type = Int.self // Int.Type is a type, and Int.self is an instance
let a = SomeClass() // what’s actually happening is SomeClass.self.init()

They are helpful for generic code. In fact, Codable & UITableView have methods using parameters of X.Type.

To be continued…

Originally published 09/15/2022 @ https://pittcsc.org/ Discord

Published here on 09/20/2022. Blog published date reflects the original date of publication.