我只想在Swift中获取单个字符字符串的ASCII值。这是我目前正在做的事情:
var singleChar = "a"
println(singleChar.unicodeScalars[singleChar.unicodeScalars.startIndex].value) //prints: 97
这太丑了。必须有一个更简单的方法。
Answers:
编辑/更新Swift 5.2或更高版本
extension StringProtocol {
var asciiValues: [UInt8] { compactMap(\.asciiValue) }
}
"abc".asciiValues // [97, 98, 99]
在Swift 5中,您可以使用新的字符属性isASCII和asciiValue
Character("a").isASCII // true
Character("a").asciiValue // 97
Character("á").isASCII // false
Character("á").asciiValue // nil
旧答案
您可以创建一个扩展:
Swift 4.2或更高版本
extension Character {
var isAscii: Bool {
return unicodeScalars.allSatisfy { $0.isASCII }
}
var ascii: UInt32? {
return isAscii ? unicodeScalars.first?.value : nil
}
}
extension StringProtocol {
var asciiValues: [UInt32] {
return compactMap { $0.ascii }
}
}
Character("a").isAscii // true
Character("a").ascii // 97
Character("á").isAscii // false
Character("á").ascii // nil
"abc".asciiValues // [97, 98, 99]
"abc".asciiValues[0] // 97
"abc".asciiValues[1] // 98
"abc".asciiValues[2] // 99
UnicodeScalar("1")!.value // returns 49
斯威夫特3.1
guard let value = UnicodeScalar("1")?.value else { return }
。
if let
或guard let
语句也存在是有原因的。我坚信,只要有可能,您就应该放心。而且,作为经验丰富的开发人员,我们应该在stackoverflow为所有新手提供良好的编码风格和最佳实践。这并不意味着犯罪。
您可以使用NSString的characterAtIndex完成此操作...
var singleCharString = "a" as NSString
var singleCharValue = singleCharString.characterAtIndex(0)
println("The value of \(singleCharString) is \(singleCharValue)") // The value of a is 97
error: value of type 'String' has no member 'character'
。但是当使用“ a”作为NSString时,它的工作是:)
斯威夫特4.1
https://oleb.net/blog/2017/11/swift-4-strings/
let flags = "99_problems"
flags.unicodeScalars.map {
"\(String($0.value, radix: 16, uppercase: true))"
}
结果:
["39", "39", "5F", "70", "72", "6F", "62", "6C", "65", "6D", "73"]
let flags = "😀"
flags.unicodeScalars.map {
"\(String($0.value, radix: 16, uppercase: true))"
}
//“ 1F600”
UInt8上还有UInt8(ascii:Unicode.Scalar)初始化程序。
var singleChar = "a"
UInt8(ascii: singleChar.unicodeScalars[singleChar.startIndex])
var singleChar = "😀"
UInt8(ascii: singleChar.unicodeScalars[singleChar.startIndex])
//致命错误:代码点值不适合ASCII:文件/AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1200.2.10.322/swift/stdlib/public/core/UnicodeScalar .swift,354行
迅捷4+
字符到ASCII
let charVal = String(ch).unicodeScalars
var asciiVal = charVal[charVal.startIndex].value
ASCII到Char
let char = Character(UnicodeScalar(asciiVal)!)
let charVal = String("😀").unicodeScalars
charVal[charVal.startIndex].value
// 128512
使用Swift 5,您可以选择以下方法之一,以获取字符的ASCII数字表示形式。
Character
的asciiValue
属性Character
有一个名为的属性asciiValue
。asciiValue
具有以下声明:
var asciiValue: UInt8? { get }
如果是ASCII字符,则此字符的ASCII编码值。
以下Playground示例代码显示了如何使用asciiValue
以获得字符的ASCII编码值:
let character: Character = "a"
print(character.asciiValue) //prints: Optional(97)
let string = "a"
print(string.first?.asciiValue) //prints: Optional(97)
let character: Character = "👍"
print(character.asciiValue) //prints: nil
Character
的isASCII
属性和Unicode.Scalar
的value
属性或者,您可以检查字符串的第一个字符是ASCII字符(使用Character
的isASCII
属性),然后获取其第一个Unicode标量的数字表示形式(使用Unicode.Scalar
的value
属性)。下面的Playground示例代码显示了如何进行:
let character: Character = "a"
if character.isASCII, let scalar = character.unicodeScalars.first {
print(scalar.value)
} else {
print("Not an ASCII character")
}
/*
prints: 97
*/
let string = "a"
if let character = string.first, character.isASCII, let scalar = character.unicodeScalars.first {
print(scalar.value)
} else {
print("Not an ASCII character")
}
/*
prints: 97
*/
let character: Character = "👍"
if character.isASCII, let scalar = character.unicodeScalars.first {
print(scalar.value)
} else {
print("Not an ASCII character")
}
/*
prints: Not an ASCII character
*/