Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift why strcmp of backspace returns -92?

I was tried to detecting backspace inside of UITextfieldDelegate. And found this answer. https://stackoverflow.com/a/49294870/911528

And It working correctly.

But I don't know what's going on inside of this function.

let  char = string.cString(using: String.Encoding.utf8)!
let isBackSpace = strcmp(char, "\\b")

if isBackSpace == -92 {
    print("Backspace was pressed")
    return false
}
  1. I don't know why the result of cString of backspace is 0. I can debug on XCode. Please check the screenshot.

enter image description here

  1. I don't know why the strcmp about 0 for backspace(as far as I got this value on XCode debugger) and "\b" character returns -92

enter image description here

I knew the ascii code of backspace is "8" of type int so I thought above function would be like this code below.

// I thought this char would be "\b" of type char or 08 of type int
// but XCode give the value 0... I don't know why
let char = string.cString(using: String.Encoding.utf8)!

// as far as I know, strcmp returns positive value for equal values.
let isBackSpace = strcmp(char, "\\b")

// so I thought this would be
if isBackSpace > 0 {
    print("Backspace was pressed")
    return false // or true
}

Does anyone who can explain about this code step-by-step? Thank you.

ref :

https://linux.die.net/man/3/strcmp

https://theasciicode.com.ar/ascii-control-characters/backspace-ascii-code-8.html


Now I can understand why people use that code, and the code above is not correct.

Try to edit correctly by reffering to @Cristik's answer.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // backspace checking
        if string == "" {
            return true // or false
        }
like image 294
AutumnSky Avatar asked Jul 29 '18 05:07

AutumnSky


People also ask

Why does SwiftUI have so many return types?

The reason for that is that SwiftUI makes heavy use of Swift’s type system in order to perform tasks like diffing, and to ensure full type safety throughout our view hierarchy — which means that we’ll end up with quite complex return types, even for simple views, since our entire view hierarchy essentially gets encoded into the type system itself.

What is the value returned by strcmp ()?

Strings are unequal Value returned by strcmp () is: 19 Less than Zero (<0): A value less than zero is returned when the first not matching character in leftStr have lesser ASCII value than the corresponding character in rightStr. If character in leftStr is lexicographically before the character of rightStr

Can we use opaque return types outside the SwiftUI realm?

Now let’s step outside the realm of SwiftUI and explore how opaque return types could be used within other contexts as well. An initial use case that we might think of could be to use an opaque return type to perform automatic type erasure-for example to be able to return some Publisher when building up a Combine-powered data pipeline, rather...

Is it possible to replicate this behavior in Swift 5?

That’s really cool, but what’s perhaps even more interesting is that we can actually replicate more or less the exact same behavior in Swift 5. 2 as well.


1 Answers

Let's analyze a little bit the code and the data that flows through that code:

  1. string is an empty string, as when you delete a character, the textfield tries to replace it by "nothing" (an empty string)
  2. due to #1, char holds only the string terminator, which is \0, in memory: [0]
  3. "\\b" translates to the \b string, in memory: [92, 98]
  4. due to #2 and #3, strcmp returns 0 - 92 => -92.

Not sure why people use this "algorithm", when they could simply check for the emptiness of the string parameter. I assume they firstly tried to use the \b escape sequence, which doesn't seem to be a valid one, so they added an extra \ character, which compiled, but resulted in an incorrect escape sequence, as \\ is the escaping sequence for the backslash character, resulting in the b part being represented as the plain letter.

Seems that backspace is not reported by the delegate method, instead the delegate method is asked to operate on strings. Detecting the actual key press might need some UIEvent/RunLoop hooks.

Note that an empty string passed to textField(_,shouldChangeCharactersIn:,replacementString:) doesn't always mean that backspace was presset, a cut operation might result too in an empty string being sent to the delegate. You could try and check the range and assume that for a range of 1 backspace was pressed, however the user can also cut one character...

like image 110
Cristik Avatar answered Sep 20 '22 19:09

Cristik