String, substring, Range, NSRange in Swift 4

I am using the following code to get a String substring from an NSRange:

func substring(with nsrange: NSRange) -> String? {
    guard let range = Range.init(nsrange)
        else { return nil }
    let start = UTF16Index(range.lowerBound)
    let end = UTF16Index(range.upperBound)
    return String(utf16[start..<end])


When I compile with Swift 4 (Xcode 9b4), I get the following errors for the two lines that declare start and end:

'init' is unavailable
'init' was obsoleted in Swift 4.0

I am confused, since I am not using an init.

How can I fix this?


Use Range(_, in:) to convert an NSRange to a Range in Swift 4.

extension String {
    func substring(with nsrange: NSRange) -> Substring? {
        guard let range = Range(nsrange, in: self) else { return nil }
        return self[range]

With Swift 4 we can get substrings this way.

  1. Substring from index

    let originStr = "Test"
    let offset = 1
    let str = String(originStr.suffix(from: String.Index.init(encodedOffset: offset)))
  2. Substring to index

    let originStr = "Test"
    let offset = 1

Need Your Help

Inno Setup: Control panel icon does not show

windows inno-setup setup-deployment ico

I have an Inno Setup project. Everything is fine, but I do not see the application icon in the "Programs and Features" control panel area. I do see the icon everywhere else.

Android Lint report <issue> duplication

android xml report lint

I run ./gradlew clean lint command in console and get Lint report in build/lint/ReleaseOutput.html and build/lint/ReleaseOutput.xml