2017-03-03 52 views
-1

Mein Code unten stürzt ab:Range-Funktion und Absturz in schnellen 3

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 
    } 

Dieser Code wird mit schnellen 3 Migration abstürzt.

Kann jemand helfen, das Problem zu beheben.

Below is the sequence of events: 

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

1) Funktion 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) Funktion 2

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

3) Funktion 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 
      } 
+0

Was ist die Fehlermeldung? – rmaddy

+1

können Sie mehr Code bereitstellen? weil ich nicht finden kann, woher das utf16 kommt. liegt diese Funktion in einer Erweiterung von 'String'? Bitte geben Sie mehr Details, damit wir Ihnen helfen können. Und ja, die 'Bereich'-Funktion hat sich ein wenig von Swift2 nach Swift3 geändert, was die automatische Xcode-Konvertierung immer nicht richtig konvertiert. –

+0

@PangHoMing Ja, das sind Funktionen in der Erweiterung von String. – user1452936

Antwort

0

Sorry, ich habe nicht aktualisiert am Wochenende . Ich habe deine Frage überprüft.

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 
    } 
} 

Aber ich kann noch swift3 Syntax, die konvertiert Ihre Funktion 2 ist implementieren:

ich Ihre Funktion 1 umsetzen kann?

Meine Frage ist,

Below is the sequence of events: 

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

Ihre Eingabe, Ihr Standort nicht 9. Wie Ihre Stringlänge 9 sein soll, die maximale Lage Du sollte 8 sein ersetzen kann?

0

Ersetzen Sie einfach UTF mit UnicodeScalars im Code das Problem behoben.

Verwandte Themen