Apple has an old example how to get the range of a whole line, given a particular character range:
Counting Lines of Text
In order to obtain the full line range of the first line, they call the following Objective-C function:
[string lineRangeForRange:NSMakeRange(0, 0)]
I tried to implement the same thing in Swift, but I can't make it work because the method signature has changed:
string.lineRange(for: NSRange(location: 0, length: 0))
throws a compiler error:
Argument type 'NSRange' (aka '_NSRange') does not conform to expected type 'RangeExpression'
RangeExpression
is some weird protocol I haven't really understood in its entirety. However, I figured that Range<Bound>
conforms to it, so I tried the following:
let range = NSRange(location: 0, length: 0)
textView.string.lineRange(for: Range<Int>(range)!)
This time I get another compiler error:
Generic parameter 'R' could not be inferred
I couldn't find any generic parameter R
, neither in Range
, nor in RangeExpression
.