Reputation: 713
Get following error after updating xcode: "Cannot invoke initializer for type 'NSRegularExpression' with an argument list of type '(pattern: String, options: NilLiteralConvertible, error: NilLiteralConvertible)'"
Following is code which cause error:
func applyStylesToRange(searchRange: NSRange) {
let normalAttrs = [NSFontAttributeName : UIFont.preferredFontForTextStyle(UIFontTextStyleBody)]
// iterate over each replacement
for (pattern, attributes) in replacements {
let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil)!
regex.enumerateMatchesInString(backingStore.string, options: nil, range: searchRange) {
match, flags, stop in
// apply the style
let matchRange = match.rangeAtIndex(1)
self.addAttributes(attributes, range: matchRange)
// reset the style to the original
let maxRange = matchRange.location + matchRange.length
if maxRange + 1 < self.length {
self.addAttributes(normalAttrs, range: NSMakeRange(maxRange, 1))
}
}
}
error is at this line:- let regex = NSRegularExpression(pattern: pattern, options: nil, error: nil)! please suggest me how to resolve it.
Upvotes: 0
Views: 576
Reputation: 10951
Check out a new initialiser
public init(pattern: String, options: NSRegularExpressionOptions) throws
It means now you must pass an options
argument and be ready to catch an error. Example:
do {
let regex = try NSRegularExpression(pattern: "pattern", options: .CaseInsensitive)
} catch {
print(error)
}
Upvotes: 0
Reputation: 27620
The initalizer is now throwable and needs an option:
do {
let regex = try NSRegularExpression(pattern: "YouPattern", options: NSRegularExpressionOptions.CaseInsensitive)
} catch {
print(error)
}
Upvotes: 0