This code is not compiling on Swift 3.3. It's showing the message: 'self' used inside 'catch' block reachable from super.init call
public class MyRegex : NSRegularExpression {
public init(pattern: String) {
do {
try super.init(pattern: pattern)
} catch {
print("error parsing pattern")
}
}
}
What could that be?
The object is not fully initialized if super.init
fails, in that case your initializer must fail as well.
The simplest solution would be to make it throw
ing:
public class MyRegex : NSRegularExpression {
public init(pattern: String) throws {
try super.init(pattern: pattern)
// ...
}
}
Or as a failable initializer:
public class MyRegex : NSRegularExpression {
public init?(pattern: String) {
do {
try super.init(pattern: pattern)
} catch {
print("error parsing pattern:", error.localizedDescription)
return nil
}
// ...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With