Let's say we have a Swift class with an initializer which can throw an error. This class must be used in Objective-C
codebase (NSObject
subclass):
import Foundation
enum EvenError : ErrorType {
case NonEvenNumber
}
class FooEven : NSObject {
var evenNumber : UInt
init(evenNumber: UInt) throws {
guard evenNumber % 2 == 0 else {
throw EvenError.NonEvenNumber
}
self.evenNumber = evenNumber
}
}
Produces compilation warning:
<unknown>:0: warning: no calls to throwing functions occur within 'try' expression
I can work around this warning in 2 ways:
- by replacing throwable initialiser (
init... -> throws
) with failable one (init?
) - giving up on subclassing from
NSObject
Yet this way I will:
- loose information about an error causing the exception,
- have to make instances of FooEven optionals and / or handle many:
if let fooEven = FooEven.init() {...}
statements - ... or I will not be able to use it in existing
Objective-C
code:
None of the above satisfies my needs / requirements.
Is there an other way to remove that warning without loosing information about the error?