Calling die()
or die("Oh no!")
prints the name of the file, the location of the error and the optional message and calls exit(EXIT_FAILURE)
afterwards. There also are multiple convenience functions to work with throwing functions:
dieOnThrow {
try maybeThrowingFunction(["Please", "don't", "throw"])
}
Or using the message parameter to supply a failure reason:
dieOnThrow("Failed to compute the answer") {
guard someValue == 42 else { throw SomeError }
}
And for working with throwing functions and using the result:
let contents = dieOnThrow("Unable to get the contents of \(someURL)") {
let contents = try fm.contentsOfDirectoryAtURL(someURL, includingPropertiesForKeys: nil, options: [])
// Do more stuff...
return contents
}
Additionaly there are functions to die in case an expression evaluates to nil, or not to nil:
let result = dieIfNil(someObject.methodThatMightReturnNil())
dieIfNotNil(someObject.methodThatReturnsAnError())
As well as for true or false:
dieIfFalse(fm.isDeletableFileAtPath(sourcePath))
dieOnThrow("Failed to delete \(sourcePath)") {
try fm.removeItemAtPath(sourcePath)
}
dieIfTrue(fm.fileExistsAtURL(sourceURL))
Add github "daehn/die"
to your Cartfile