Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegularExpression matchesInString issue in Swift

I am converting a CoreText based app to Swift and I am facing an issue when getting the matches to a regular expression in the text.

This is the sample code

let regexOptions = NSRegularExpressionOptions.CaseInsensitive | NSRegularExpressionOptions.DotMatchesLineSeparators
let regex = NSRegularExpression.regularExpressionWithPattern("(.*?)(<[^>]+>|\\Z)", options: regexOptions, error: nil)
var results: Array<NSTextCheckingResult> = regex.matchesInString(text, options: 0, range: NSMakeRange(0, countElements(text)))

According to the documentation, the matchesInString function returns an array of NSTextCheckingResults, but the compiler complains stating that "The Expression of type anyObject[] can´t be converted to "NSMatchingOptions". Any idea of what might be wrong here?

like image 775
eharo2 Avatar asked Mar 20 '23 07:03

eharo2


1 Answers

Try assigning to your results variable like this:

var results = regex.matchesInString(text, options: nil, range: NSMakeRange(0, countElements(text))) as Array<NSTextCheckingResult>

  1. the return type is Array<AnyObject>[]!, you can cast here (as in the above example) or later when you check the members of the collection

  2. in Swift options take nil to represent an empty option set (vs. 0 in Objective-C)

like image 172
fqdn Avatar answered Mar 22 '23 23:03

fqdn