Day #73

Metatypes (Part 1 of X)

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/, republished 09/20/2022.

Licensed under CC BY-NC-SA 4.0
Last updated on Sep 15, 2022 00:00 UTC
Built with Hugo
Theme Stack designed by Jimmy