Want Query Want Query - 7 months ago 279
Swift Question

Detect backspace Event in UITextField

I am searching for solutions on how to capture a backspace event, most Stack Overflow answers are in Objective-C but I need on Swift language.

First I have set delegate for the UITextField and set it to self

self.textField.delegate = self;

Then I know to use
delegate method to detect if a backspace was pressed is all code are in Objective-C. I need in Swift these following method as below is used.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
int isBackSpace = strcmp(_char, "\b");

if (isBackSpace == -8) {
// NSLog(@"Backspace was pressed");

return YES;


Hope will help you :p

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let  char = string.cStringUsingEncoding(NSUTF8StringEncoding)!
    let isBackSpace = strcmp(char, "\\b")

    if (isBackSpace == -92) {
        println("Backspace was pressed")
    return true