In swift 4.2 I am trying to write the following code:
let regex = NSRegularExpression(
pattern: "\w[A-Z]",
options: NSRegularExpression.Options.allowCommentsAndWhitespace
)
This initializer shows up in the autocomplete, but when I try to build, I get the following 2 errors:
- Incorrect argument label in call (have 'options:', expected 'coder:')
- Invalid escape sequence in literal
It seems like a bug, but I could also be doing something wrong!
Solved!
1. The problem was in the xcode autocomplete, for swift 4.2 the API is actually
NSRegularExpression(pattern: "")
2. The invalid escape sequence was due to needing to escape the backslash.
So the following works:
let regex = try NSRegularExpression(pattern: "\\w[A-Z]")