Cannot convert value of type 'NSRange' (aka '_NSRange') to expected type 'Range'(aka 'Range')

后端 未结 2 1304
长情又很酷
长情又很酷 2021-02-19 00:15

Getting this error when checking the range for string characters...

@objc func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, re         


        
相关标签:
2条回答
  • 2021-02-19 00:30

    Swift 4

    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
    
        if let oldString = textField.text {
            let newString = oldString.replacingCharacters(in: Range(range, in: oldString)!,
                                                          with: string)
            // ...
        }
        // ...
    }
    
    0 讨论(0)
  • 2021-02-19 00:34

    Instead of text! say (text! as NSString).

    var newString = (text! as NSString).stringByReplacingCharactersInRange(range, withString: string) as? NSString
    
    0 讨论(0)
提交回复
热议问题