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

后端 未结 2 1301
长情又很酷
长情又很酷 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)
            // ...
        }
        // ...
    }
    

提交回复
热议问题