I haven't read too much into Swift but one thing I noticed is that there are no exceptions. So how do they do error handling in Swift? Has anyone found anything related to error-handling?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
Edit: Although this answer works, it is little more than Objective-C transliterated into Swift. It has been made obsolete by changes in Swift 2.0. Guilherme Torres Castro's answer above is a very good introduction to the preferred way of handling errors in Swift. VOS
It took a bit of figuring it out but I think I've sussed it. It seems ugly though. Nothing more than a thin skin over the Objective-C version.
Calling a function with an NSError parameter...
Writing the function that takes an error parameter...
What I have seen is that because of the nature of the device you don't want to be throwing a bunch of cryptic error handling messages at the user. That is why most functions return optional values then you just code to ignore the optional. If a function comes back nil meaning it failed you can pop a message or whatever.
Starting with Swift 2, as others have already mentioned, error handling is best accomplished through the use of do/try/catch and ErrorType enums. This works quite well for synchronous methods, but a little cleverness is required for asynchronous error handling.
This article has a great approach to this problem:
https://jeremywsherman.com/blog/2015/06/17/using-swift-throws-with-completion-callbacks/
To summarize:
then, the call to the above method would be as follows:
This seems a bit cleaner than having a separate errorHandler callback passed to the asynchronous function, which was how this would be handled prior to Swift 2.
Nice and simple lib to handle exception: TryCatchFinally-Swift
Like a few others it wraps around the objective C exception features.
Use it like this:
As Guilherme Torres Castro said, in Swift 2.0,
try
,catch
,do
can be used in the programming.For example, In CoreData fetch data method, instead of put
&error
as a parameter into themanagedContext.executeFetchRequest(fetchRequest, error: &error)
, now we only need to use usemanagedContext.executeFetchRequest(fetchRequest)
and then handle the error withtry
,catch
(Apple Document Link)If you have already download the xcode7 Beta. Try to search throwing errors in Documentations and API Reference and choose the first showing result, it gives a basic idea what can be done for this new syntax. However, fully documentation is not post for many APIs yet.
More fancy Error Handling techniques can be found in
Basic wrapper around objective C that gives you the try catch feature. https://github.com/williamFalcon/SwiftTryCatch
Use like: