Expression RegularExpressionInString in Swift

I am converting a CoreText-based application to Swift and am having trouble getting regular expression matches in the text.

This is 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 function matchesInStringreturns an array NSTextCheckingResults, but the compiler complains that "an expression like anyObject [] cannot be converted to NSMatchingOptions". "Any idea what might be wrong here?

+4
source share
2 answers

Try assigning the variable resultsas follows:

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

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

  • Swift nil (vs. 0 Objective-C)

+7

, , , , . , , , , , , . , @fqdn. , (\ u {A}) countElements. , .unicodeScalars , , , .

println(countElements("\u{A}\u{A}\u{A}\n\u{D}\n\u{D}\n\u{D}\n\u{D}\n")) //8
println(countElements("\u{A}\u{A}\u{A}\n\u{D}\n\u{D}\n\u{D}\n\u{D}\n".unicodeScalars))  //12

: ​​ .

0

Source: https://habr.com/ru/post/1545129/


All Articles