NSRange to Range

前端 未结 13 1417
失恋的感觉
失恋的感觉 2020-11-22 11:59

How can I convert NSRange to Range in Swift?

I want to use the following UITextFieldDelegate method:

13条回答
  •  我在风中等你
    2020-11-22 12:22

    In the accepted answer I find the optionals cumbersome. This works with Swift 3 and seems to have no problem with emojis.

    func textField(_ textField: UITextField, 
          shouldChangeCharactersIn range: NSRange, 
          replacementString string: String) -> Bool {
    
      guard let value = textField.text else {return false} // there may be a reason for returning true in this case but I can't think of it
      // now value is a String, not an optional String
    
      let valueAfterChange = (value as NSString).replacingCharacters(in: range, with: string)
      // valueAfterChange is a String, not an optional String
    
      // now do whatever processing is required
    
      return true  // or false, as required
    }
    

提交回复
热议问题