I am attempting to pass an error pointer in swift and am unable to do so. The compiler complains that "NSError is not convertible to 'NSErrorPointer'".
var error: NSError = NSError() var results = context.executeFetchRequest(request, error: error) if(error != nil) { println("Error executing request for entity \(entity)") }
There are four ways to handle errors in Swift. You can propagate the error from a function to the code that calls that function, handle the error using a do - catch statement, handle the error as an optional value, or assert that the error will not occur.
Swift doesn't support throwing exceptions, nor does it support catching them. This wouldn't be a problem if you could develop iOS apps in pure Swift, but unfortunately at the moment you cannot.
Information about an error condition including a domain, a domain-specific error code, and application-specific information.
Swift Disabling an Error Propagation In those cases, we can use try! before an expression to disable error propagation. During execution, if no error occurred then try! will return the value in case if any error occurred, we will get a runtime error. Following is the example of disabling an error propagation using try!
You just pass a reference like so:
var error: NSError? var results = context.executeFetchRequest(request, error: &error) if error != nil { println("Error executing request for entity \(entity)") }
Two important points here:
NSError?
is an optional (and initialized to nil
)&
operator (e.g., &error
)See: Using swift with cocoa and objective-c
This suggestion is up for discussion, but some engineers would prefer to use the golden path syntax:
var maybeError: NSError? if let results = context.executeFetchRequest(request, error: &maybeError) { // Work with results } else if let error = maybeError { // Handle the error }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With