2015-09-24 8 views
18

Ottenere questo errore quando si controlla l'intervallo per caratteri stringa ...Impossibile convertire il valore di tipo "NSRange" (aka "_NSRange") nel tipo previsto "Intervallo <Index>" (noto anche come "Intervallo <String.CharacterView.Index>")

@objc func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    let shouldChange = false 
    let text = textField.text 
    var newString = text!.stringByReplacingCharactersInRange(range, withString: string) as? NSString 
    if newString.length > 14{ 
     newString = newString.substringToIndex(14) 
    } 
    textField.text = newString.uppercaseString 

    return shouldChange 
} 
+0

Compare [NSRange alla gamma ] (http://stackoverflow.com/questions/25138339/nsrange-to-rangestring-index). –

risposta

33

Invece di text! dicono (text! as NSString).

var newString = (text! as NSString).stringByReplacingCharactersInRange(range, withString: string) as? NSString