I'm sorry, I am newbie in Swift, but I have about 5 years of development in Objective-C.
Here is the deal. I have frequently used regular expression, so I decide to write extension with method which returns regex with predefined pattern and options. But in my case NSRegularExpression initialization demands error handling. Because of that I have to declare a variable before init. But the type of this variable should be Self (because it's class method and it should return Self)!
So my code may be look like that:
class func frequentlyUsedExpression() -> Self {
let pattern = "frequently_used_pattern"
var regex : Self? = nil
do {
regex = try self(pattern: pattern, options: [])
} catch let error as NSError {
print(error)
regex = nil
}
return regex
}
But I cannot create Self-type variable.
I cannot ignore error handling even if I would. Because of that I cannot do "return self(pattern: pattern, options: [])", which can be used in other cases.
I cannot declare regex as NSRegularExpression, because it's impossible to cast it in Self in return.
I cannot create convenience initializer, because my method have no parameters.
I spent about three hours trying to solve it, but still don't know what to do.