10

In my application, I have some code to fetch the range of the host in a URL. It looks like this:

private func rangeOfHost(text: String) -> NSRange? {
    let url = URL(string: text)
    if let host: String = url?.host {
        if let range = text.range(of: host) {
            return NSRange(
                location: range.lowerBound.encodedOffset,
                length: range.upperBound.encodedOffset - range.lowerBound.encodedOffset
            )
        }
    }
    return nil
}

Xcode has been warning me that 'encodedOffset' is deprecated: encodedOffset has been deprecated as the most common usage is incorrect. Use utf16Offset(in:) to achieve the same behavior.. However, it's not clear to me how I can replace those encodedOffsets with these suggestions. Any ideas?

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
user4992124
  • 1,574
  • 1
  • 17
  • 35

2 Answers2

8

A simple and correct way to create an NSRange from a Range<String.Index> is to use its initializer:

public init<R, S>(_ region: R, in target: S) where R : RangeExpression, S : StringProtocol, R.Bound == String.Index

In your case:

if let range = text.range(of: host) {
    return NSRange(range, in: text)
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
2
yourString1.yourIndex.utf16Offset(in: yourString2)
Hiren Panchal
  • 2,963
  • 1
  • 25
  • 21
  • I've found that for me that the value of yourString2 can be anything at all, and I still get the result I expect. So I don't understand what that string is for, and I worry that in the future it will suddenly matter and things will break. The docs, of course, are super helpful: "No description." – John Endres Feb 23 '21 at 16:50