Saravana kumar Saravana kumar - 2 months ago 17
Swift Question

Swift: Value of type 'string' has no member 'length'

I Have Implement Some TextField Functionalities in my App so I used TextField Delegates but it show some error like "Value of type string has no member length" I don't know how to solve this please help me for fix this issue.

Here I Give the Code of What I am trying.

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
}

Answer

Replace

if string.length > 0

by

if string.characters.count > 0