-1

My below code crashes:

func getrange(_ from: Int, length: Int) -> Range<String.Index>? {
    guard let fromU16 = utf16.index(utf16.startIndex, offsetBy: from, limitedBy: utf16.endIndex), fromU16 != utf16.endIndex else {
      return nil   ----->crashes here
    }
    let toU16 = utf16.index(fromU16, offsetBy: length, limitedBy: utf16.endIndex) ?? utf16.endIndex
    guard let from = String.Index(fromU16, within: self),
      let to = String.Index(toU16, within: self) else { return nil }
    return from ..< to
  }

This code is crashing with swift 3 migration.

Can someone help debugging the issue.

Below is the sequence of events:

     //input for below function is: text “123456789”, string “0”, nsrange = location =9, length=0

1) function 1

static func numericText(_ text: String, replacedBy string: String, in nsrange: NSRange) -> String {

            guard let range = text.range(for: nsrange) else {
              //assertionFailure("Should never reach here")
              return text.numericString()
            }

            // Apply Replacement String to the textField text and extract only the numeric values
            return text.replacingCharacters(in: range, with: string)
              .numericString()
          }

2) function 2

        func range(for nsrange: NSRange) -> Range<String.Index>? {
            return range(nsrange.location, length: nsrange.length)
          }

3) function 3

        func range(_ from: Int, length: Int) -> Range<String.Index>? {
            guard let fromU16 = utf16.index(utf16.startIndex, offsetBy: from, limitedBy: utf16.endIndex), fromU16 != utf16.endIndex else {
              return nil
            }
            let toU16 = utf16.index(fromU16, offsetBy: length, limitedBy: utf16.endIndex) ?? utf16.endIndex
            guard let from = String.Index(fromU16, within: self),
              let to = String.Index(toU16, within: self) else { return nil }
            return from ..< to
          }
user1452936
  • 555
  • 1
  • 6
  • 14
  • What's the error message? – rmaddy Mar 03 '17 at 02:40
  • 1
    can you provide more code? as I cant find where does the `utf16` come from. is this function locations in an extension of `String`? please provide more detail so tat we can help you. And yes the `range` function has changed a bit from Swift2 to Swift3, which the Xcode auto converttion always fails to convert it properly. – Pang Ho Ming Mar 03 '17 at 02:54
  • @PangHoMing yes these are function in extension of String. – user1452936 Mar 03 '17 at 15:03
  • Please provide code in such a way that it can be copied and pasted into an iOS project and tested directly (MVCE = minimal verifiable complete example). http://stackoverflow.com/help/mcve – matt Mar 03 '17 at 15:16
  • still need more code. still doesn't know the data type of `utf16`. In fact you can post the whole class to the post or a link to your github repo? – Pang Ho Ming Mar 03 '17 at 15:19
  • @PangHoMing sorry if i am not getting ur que, but i dint define any type explicitly for utf16, my fromU16 is String.UTF16View.Index. i printed po utf16 and i got below: StringUTF16("1234 5678") - 0 : 49 - 1 : 50 - 2 : 51 - 3 : 52 - 4 : 32 - 5 : 53 - 6 : 54 - 7 : 55 - 8 : 56 – user1452936 Mar 03 '17 at 16:43

2 Answers2

0

Sorry, I didn't update during the weekend. I reviewed your question.

I can implement your function 1:

extension String {
    func getrange(_ from: Int, length: Int) -> Range<String.Index>? {
        guard let fromU16 = utf16.index(utf16.startIndex, offsetBy: from, limitedBy: utf16.endIndex), fromU16 != utf16.endIndex else {
            return nil
        }
        let toU16 = utf16.index(fromU16, offsetBy: length, limitedBy: utf16.endIndex) ?? utf16.endIndex
        guard let from = String.Index(fromU16, within: self),
            let to = String.Index(toU16, within: self) else { return nil }
        return from ..< to
    }
}

But I cant implement your function 2, is that converted to Swift3 syntax yet?

My question is this,

Below is the sequence of events:

     //input for below function is: text “123456789”, string “0”, nsrange = location =9, length=0

your input, your location shouldn't be 9. As your string length is 9, the max location your can replace should be 8?

Pang Ho Ming
  • 1,299
  • 10
  • 29
0

Just replacing utf with unicodeScalars in the code fixed the issue.

user1452936
  • 555
  • 1
  • 6
  • 14