Reputation: 11
I was creating a Regular expression but in can´t solve this problem
class Regex {
let internalExpression: NSRegularExpression
let pattern: String
init(_ pattern: String) {
self.pattern = pattern
var error: NSError?
self.internalExpression = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)
}
func test(input: String) -> Bool {
let matches = self.internalExpression.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input)))
return matches.count > 0
}
}
Here are the errors I got:
"Cannot invoke initializer for type 'NSRegularExpression' with an argument list of type '(pattern: String, options: _, error: inout NSError?)'"
and
"Use of unresolved identifier 'countElements'"
I only want to use this:
if usuarioView.text! =~ "/^[a-zA-Z0-9_]{3,30}$/"
Upvotes: 1
Views: 1363
Reputation: 42449
There are a few issues with your code. It looks like you are migrating from an earlier version of Swift to Swift 2.x.
The NSRegularExpression
initializer has changed to throw its error instead of passing in an ErrorPointer
:
do {
self.internalExpression = try NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
} catch {
print(error)
}
To specific nil
for an OptionType
value, use []
. To get the length of a string, use input.characters.count
, since countElements
has been removed from Swift:
let matches = self.internalExpression.matchesInString(input, options: [], range:NSMakeRange(0, input.characters.count))
Upvotes: 0