我在我的應用中實現了一些TextField功能,所以我使用了TextField Delegates,但它顯示了一些錯誤,如「字符串類型的值沒有成員長度」不知道如何解決這個問題,請幫我解決這個問題。Swift:'string'類型的值沒有成員'length'
在這裏,我給出了我所嘗試的代碼。
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var oldLength: Int = textField.text!.characters.count
var replacementLength: Int = string.characters.count
var rangeLength: Int = range.length
var newLength: Int = oldLength - rangeLength + replacementLength
if textField.tag == 1 {
if string.length > 0 && !NSScanner.scannerWithString(string).scanInt(nil) { **------>//Error was showed in this line.**
return false
}
// This 'tabs' to next field when entering digits
if newLength == 5 {
if textField == txtOtp4 {
textField.resignFirstResponder()
}
}
else if oldLength > 0 && newLength == 0 {
}
return newLength <= 4
}
else {
if newLength == 11 {
if textField == txtPhoneNumber {
textField.resignFirstResponder()
}
return newLength <= 10
}
}
return true
// This allows numeric text only, but also backspace for deletes
}
爲什麼你在一行中使用'string.length',而在另一行中使用'string.characters.count'? – luk2302