我在我的应用程序中实现了一些文本字段功能,因此我使用了TextField Delegates,但是它显示了一些错误,例如“类型为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