What is the canonical way to get the Wrapped type from an Optional?

My use case is as following:

class myObject: NSObject {
    // ...
    init() {
        setupAsyncHandler { [weak self] in
             type(of: self).myMethod(weakInstance: { [weak self] in self }) // <-- how to fix?

    class func myMethod(weakInstance: () -> SomeObject?) { /*...*/ }

I want to allow subclasses to override myMethod, thus I need to dynamically resolve a type of the self optional but I'm not sure how to get a wrapped type out of an Optional.

Wrapped is not technically a member of Optional because it’s the name of the generic parameter. So I have a not so great solution:

extension Optional {
  var wrappedType: Wrapped.Type {

// inside the closure

I’m sure others have better answers!

1 Like

What do you expect subclasses to do with the type? Are they checking for some sort of conformance at runtime? Are they altering their behavior based on what type of object it is? Both of these can be solved with as? or is casting to specific types.

I merely need to call the appropriate class method instead of pinning myObject.myMethod for the whole hierarchy.