8

I need to be able to create a String that is "\b". But when I try to, Xcode throws a compile-time error: Invalid escape sequence in literal. I don't understand why though, "\r" works just fine. If I put "\\b" then that's what is actually stored in the String, which is not what I need - I only need one backslash. To me, this seems to be a Swift oddity because it works just fine in Objective-C.

let str = "\b" //Invalid escape sequence in literal
NSString *str = @"\b"; //works great

I need to generate this string because "\b" is the only way to detect when the user pressed 'delete' when using UIKeyCommand:

let command = UIKeyCommand(input: "\b", modifierFlags: nil, action: "didHitDelete:")

How can I work around this issue?

EDIT: It really doesn't want to generate a String that is only "\b", this does not work - it stays the original value:

var delKey = "\rb"
delKey = delKey.stringByReplacingOccurrencesOfString("r", withString: "", options: .LiteralSearch, range: nil)
Jordan H
  • 52,571
  • 37
  • 201
  • 351
  • Incidentally, your string replacement doesn't work because in Swift, "\rb" is a carriage return character followed by the letter "b" (literally the bytes 0D 98). That string doesn't have an "r" in it to replace... – Matt Gibson Dec 22 '14 at 20:57

2 Answers2

11

The Swift equivalent of \b is \u{8}. It maps to ASCII control code 8, just like \b in Objective C. I've tested this and found it to work fine with UIKeyCommand, in this earlier answer of mine.

Example snippet:

func keyCommands() -> NSArray {
    return [
        UIKeyCommand(input: "\u{8}", modifierFlags: .allZeros, action: "backspacePressed")
    ]
}
Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
5

I don't believe it is supported.

Based on the Swift documentation https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html:

String literals can include the following special Unicode characters:

The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)

An arbitrary Unicode scalar, written as \u{n}, where n is between one and eight hexadecimal digits

The ASCII for the \b is 8. If you do the following, you'll see these results

let bs = "\u{8}"
var str = "Simple\u{8}string"

println(bs) // Prints ""
println("bs length is \(bs.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))") // Prints 1
println(str) // Prints Simplestring

let space = "\u{20}"

println(space) // Prints " "
println("space length is \(space.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))") // Prints 1

str = "Simple\u{20}string"
println(str) // Prints Simple string

It looks like while ASCII 8 "exists", it is "ignored".

Mobile Ben
  • 7,121
  • 1
  • 27
  • 43
  • So this is simply impossible at this point in time, there's no way to workaround it? – Jordan H Dec 08 '14 at 18:08
  • Sorry dude it is sort of looking like that. What are you trying to do with the backspace? Perhaps there is another way of faking it by using a hidden UITextFields and something like http://stackoverflow.com/questions/1977934/detect-backspace-in-uitextfield/6637821#6637821. Or you could always try and figure out what the value is by trying something like this: https://gist.github.com/steventroughtonsmith/7515380. You can see the what is returned on pressing backspace. – Mobile Ben Dec 09 '14 at 06:37