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?
Try assigning to your results
variable like this:
var results = regex.matchesInString(text, options: nil, range: NSMakeRange(0, countElements(text))) as Array<NSTextCheckingResult>
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
in Swift options take nil
to represent an empty option set (vs. 0
in Objective-C)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With