Skip to main content

Articles, snippets and thoughts about code from Andrew Lord.

Using metatype Self to return current type

Published on · Updated on
1 minute read

A helpful keyword in Swift is Self, which is a metatype that represents the current type. The current type may be the class, subclass or current implementer of a protocol, depending on where it is used.

A handy use-case for it is to avoid specifying generic placeholder types. Neat!

class ThreadRobot<CallerT: CallerRobot> {
    private let app = XCUIApplication()

    func tapCompleteButton() -> Self {
        return self

I hope the article was useful. If you have any feedback or questions please feel free to reach out to me on Twitter.

Thanks for reading and happy coding!

Like what you read? Please share the article.

Avatar of Andrew Lord

Hi, I hope you enjoyed the article. I am Andrew - a builder of apps and developer tools. Articles on the blog focus on all aspects of Android and iOS development using Kotlin and Swift.

Subscribe via RSS