Learn how Cocoa error parameters are converted to Swift throwing methods.
In Cocoa, methods that produce errors take an
NSError pointer parameter as their last parameter, which populates its argument with an
NSError object if an error occurs. Swift automatically translates Objective-C methods that produce errors into methods that throw an error according to Swift’s native error handling functionality.
Understand How Error Parameters Are Imported
Swift examines Objective-C method declarations and translates them into Swift throwing methods, with shorter names when possible.
In Swift, it’s imported like this:
Notice that the
remove method is imported by Swift with a
Void return type, no error parameter, and a
If the last non-block parameter of an Objective-C method is of type
NSError **, Swift replaces it with the throws keyword, to indicate that the method can throw an error. If the Objective-C method’s error parameter is also its first parameter, Swift attempts to simplify the method name further, by removing the
And suffix, if present, from the first part of the selector. If another method is declared with the resulting selector, the method name is not changed.
If an error producing Objective-C method returns a
BOOL value to indicate the success or failure of a method call, Swift changes the return type of the function to
Void. Similarly, if an error producing Objective-C method returns a
nil value to indicate the failure of a method call, Swift changes the return type of the function to a nonoptional type.
Otherwise, if no convention can be inferred, the method is left intact.