Reputation: 6821
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)")
}
Upvotes: 56
Views: 25070
Reputation: 76918
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
Upvotes: 100
Reputation: 1357
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
}
Upvotes: 10