I'm trying to write a initializer for NSURL which can throw on failure.

I would very much like it to look like [this](https://developer.apple.com/swift/blog/?id=17).

```
extension NSURL {
    convenience init?(string: String, throwOnNil: Bool) throws {
        if let URL = NSURL(string: string) {
            self = URL
        } else if throwOnNil {
            throw MyURLError(string: string)
        } else {
            return nil
        }
    }
}

```

But that syntax is no longer supported: `error: cannot assign to value: 'self' is immutable`.

*That syntax only works for struct and enum.*

Here is the only thing I've come up with

```
extension NSURL {
    convenience init?(string: String, throwOnNil: Bool) throws {
        if throwOnNil && NSURL(string: string) == nil {
            throw MyURLError(string: string)
        }

        self.init(string: string)
    }
}

```

Which constructs the URL twice.

***HELP!*** There must be a better approach that I'm not finding.

---

**UPDATE**

It turns out that self assignment in an initializer works for structs and enums but not for classes. The example I was basing my ideal code from was a struct.