# Allow \`self = x\` in class convenience initializers

**URL:** <https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924>\
**Category:** Pitches\
**Created:** [September 6, 2018, 9:19pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924 "2018-09-06T21:19:33Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![Moximillian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moximillian/32/3055_2.png) [@Moximillian](https://forums.swift.org/u/Moximillian)\
**Post date:** [September 7, 2018, 5:08pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/16 "2018-09-07T17:08:26Z")

</div>

> [@DevAndArtist](#):
>
> ```
> // After:
> extension UIView {
> static func instantiateFromXib() -> Self {
> let metatype = Self.self
> let bundle = Bundle(for: metatype)
> let nib = UINib(nibName: "\(metatype)", bundle: bundle)
> guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {
> fatalError("Could not load view from nib file.")
> }
> return view
> }
> }
> 
> ```

As pointed out by @ahti in [Workarounds for Self in classes](https://forums.swift.org/t/workarounds-for-self-in-classes/15754), for static methods **self** is equivalent to **self.Type**. So your example above can be simpler:

```
// After:
extension UIView {
  static func instantiateFromXib() -> Self {
    let bundle = Bundle(for: self)
    let nib = UINib(nibName: "\(self)", bundle: bundle)
    guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {
      fatalError("Could not load view from nib file.")
    }
    return view
  }
}

```

However, what is missing is coercing to Self. Neither **as? self** nor **as? Self** is ok in classes, but latter works fine in protocols. On the other hand, is there a way to workaround this if one could assign to self? I guess there would still be type mismatch?

---

_[View the full topic](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924)._
