Avatar ·

How to convert NSRange to Range<String.Index> in Swift for working with NSString?

Hi everyone! I just started learning Swift and encountered a problem when working with text. I have an NSString, and I get a range (NSRange) after searching for a substring using the range(of:) method. But when I try to use this range to extract a substring from a Swift String, the compiler gives an error. Here's my code: ```swift let nsString: NSString = "Привет, мир! Hello, world!" let searchRange = nsString.range(of: "мир") // searchRange has type NSRange let swiftString: String = nsString as String // Trying to get a substring let substring = swiftString[searchRange] // Error! ``` The error says that `NSRange` cannot be used as an index for `String`. I've read that in Swift, strings are indexed via `String.Index`, not via integers or `NSRange`. How can I