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

I implemented some TextField functionality in my application, so I used TextField Delegates, but it found some error, for example: "A value of type string does not have a member length." I do not know how to solve this, please help me fix this problem.

Here I give the code of what I'm 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 } 
+5
source share
2 answers

Replace

 if string.length > 0 

by

 if string.characters.count > 0 
+13
source

If you are using Swift 2.0, you should use

 string.characters.count 

to get the number of characters per line.

+8
source

Source: https://habr.com/ru/post/1243829/


All Articles