[swift-evolution] [Accepted] SE-0112: Improved NSError Bridging

Charles Srstka cocoadev at charlessoft.com
Fri Aug 5 20:10:45 CDT 2016


> On Aug 5, 2016, at 7:16 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Would it kill to allow:
> 
> let err = NSError()
> err.localizedDescription = "bad things happen"
> throw err
> 
> or even
> 
> throw NSError("Bad things happen")

You can make something that can do that fairly easily:

struct TextualError: LocalizedError {
    var errorString: String
    
    init() {
        self.init("")
    }
    
    init(_ string: String) {
        self.errorString = string
    }
    
    var failureReason: String? {
        return self.errorString
    }
}

Then, you can just:

throw TextualError("Must Sterilize!”)

Or:

var error = TextualError()
error.failureReason = "Must Sterilize!”
throw error

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160805/95c121bb/attachment.html>


More information about the swift-evolution mailing list