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)