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.
我需要能够创建一个“\b”的字符串。但是当我尝试时,Xcode会抛出一个编译时错误:在字面上是无效的转义序列。我不明白为什么,"\r"工作很好。如果我把"\\b"放在字符串中,这不是我需要的,我只需要一个反斜杠。对我来说,这似乎是一种快速的怪异,因为它在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
:
我需要生成这个字符串,因为“\b”是在使用UIKeyCommand时用户按下“delete”的唯一方法:
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:
编辑:它真的不想生成一个只有“\b”的字符串,这不会起作用——它保持原来的值:
var delKey = "\rb"
delKey = delKey.stringByReplacingOccurrencesOfString("r", withString: "", options: .LiteralSearch, range: nil)
2 个解决方案
#1
8
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.
Swift等价的\b是\u{8}。它映射到ASCII控制代码8,就像Objective c中的\b一样。我已经测试过了,在我之前的回答中发现它可以用UIKeyCommand (UIKeyCommand)运行。
Example snippet:
示例代码片段:
func keyCommands() -> NSArray {
return [
UIKeyCommand(input: "\u{8}", modifierFlags: .allZeros, action: "backspacePressed")
]
}
#2
3
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:
基于Swift文档的https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html:
String literals can include the following special Unicode characters:
字符串文字可以包括下列特殊的Unicode字符:
The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
转义字符\0(空字符)\(反斜杠)\t(水平标签)\n(换行)\r(回车)\ \“(双引号)和\”(单引号)
An arbitrary Unicode scalar, written as \u{n}, where n is between one and eight hexadecimal digits
一个任意的Unicode标量,写为\u{n},其中n位于1到8个十六进制数字之间。
The ASCII for the \b is 8. If you do the following, you'll see these results
\b的ASCII码是8。如果您执行以下操作,您将看到这些结果。
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".
它看起来像ASCII 8“存在”,它是“被忽略”。
#1
8
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.
Swift等价的\b是\u{8}。它映射到ASCII控制代码8,就像Objective c中的\b一样。我已经测试过了,在我之前的回答中发现它可以用UIKeyCommand (UIKeyCommand)运行。
Example snippet:
示例代码片段:
func keyCommands() -> NSArray {
return [
UIKeyCommand(input: "\u{8}", modifierFlags: .allZeros, action: "backspacePressed")
]
}
#2
3
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:
基于Swift文档的https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html:
String literals can include the following special Unicode characters:
字符串文字可以包括下列特殊的Unicode字符:
The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
转义字符\0(空字符)\(反斜杠)\t(水平标签)\n(换行)\r(回车)\ \“(双引号)和\”(单引号)
An arbitrary Unicode scalar, written as \u{n}, where n is between one and eight hexadecimal digits
一个任意的Unicode标量,写为\u{n},其中n位于1到8个十六进制数字之间。
The ASCII for the \b is 8. If you do the following, you'll see these results
\b的ASCII码是8。如果您执行以下操作,您将看到这些结果。
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".
它看起来像ASCII 8“存在”,它是“被忽略”。