Free Republic
Browse · Search
General/Chat
Topics · Post Article

To: Beowulf9

This may help.

https://developer.apple.com/documentation/swift/cocoa_design_patterns/about_imported_cocoa_error_parameters

About Imported Cocoa Error Parameters
Learn how Cocoa error parameters are converted to Swift throwing methods.
On This Page

Overview
See Also
Overview
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.

For example, consider the removeItem(at:) method from FileManager. In Objective-C, it’s declared like this:

- (BOOL)removeItemAtURL:(NSURL *)URL
error:(NSError **)error;
In Swift, it’s imported like this:

func removeItem(at: URL) throws
Notice that the removeItem(at:) method is imported by Swift with a Void return type, no error parameter, and a throws declaration.

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 WithError or AndReturnError 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.

Note

You use the NS_SWIFT_NOTHROW macro on Objective-C method declarations that produce an NSError to prevent it from being imported by Swift as a method that throws.


11 posted on 03/05/2021 4:16:51 PM PST by Larry Lucido (Donate! Don't just post clickbait!)
[ Post Reply | Private Reply | To 1 | View Replies ]


To: Larry Lucido

“This may help.”

Not for someone that doesn’t even know what iOS is!


13 posted on 03/05/2021 4:18:13 PM PST by TexasGator (Z1z)
[ Post Reply | Private Reply | To 11 | View Replies ]

To: Beowulf9

(I have no idea what any of that meant, my FRiend)


14 posted on 03/05/2021 4:18:14 PM PST by Larry Lucido (Donate! Don't just post clickbait!)
[ Post Reply | Private Reply | To 11 | View Replies ]

To: Larry Lucido

Only helpful if you are a Swift developer, and if you’re a Swift developer, you probably already know this.


15 posted on 03/05/2021 4:18:20 PM PST by dinodino ( )
[ Post Reply | Private Reply | To 11 | View Replies ]

To: Larry Lucido

There goes that donation

;)


24 posted on 03/05/2021 4:26:06 PM PST by VeniVidiVici (Biden's favorite word: What?)
[ Post Reply | Private Reply | To 11 | View Replies ]

To: Larry Lucido

Funny.

Cruel.

But funny.


26 posted on 03/05/2021 4:29:23 PM PST by absalom01 (You should do your duty in all things. You cannot do more, and you should never wish to do less.)
[ Post Reply | Private Reply | To 11 | View Replies ]

To: Larry Lucido

🤪


41 posted on 03/05/2021 4:41:09 PM PST by Beowulf9
[ Post Reply | Private Reply | To 11 | View Replies ]

To: Larry Lucido

You forgot your /sarc tag.


59 posted on 03/05/2021 5:24:46 PM PST by Disambiguator
[ Post Reply | Private Reply | To 11 | View Replies ]

To: Larry Lucido

Can you translate that into computer illiterate?


74 posted on 03/06/2021 7:09:23 PM PST by smokingfrog ( sleep with one eye open (<o> --- )
[ Post Reply | Private Reply | To 11 | View Replies ]

Free Republic
Browse · Search
General/Chat
Topics · Post Article


FreeRepublic, LLC, PO BOX 9771, FRESNO, CA 93794
FreeRepublic.com is powered by software copyright 2000-2008 John Robinson