在 Swift 中更容易使用 UTF16 代理。
var emoji1 = SwiftySurrogate.decodeFromSurrogatePair(surrogatePair: "D83D:DCC9")
var emoji2 = SwiftySurrogate.decodeFromSurrogatePair(high: 0xD83C, low: 0xDF80)
您可以在代理对与 Unicode 标量之间进行转换。
expect("D83D:DE04") == "\(SwiftySurrogate.convUnicodeScalarToSurrogatePair(0x1F604).0!.hexExpression()):\(SwiftySurrogate.convUnicodeScalarToSurrogatePair(0x1F604).1!.hexExpression())"
// true
expect(UInt32(0x1F604)) == SwiftySurrogate.convSurrogateToUnicodeScalar("D83D:DE04")
// true
通过可选检查捕获解码错误。
// This will fail
if let res = SwiftySurrogate.decodeFromSurrogatePair(surrogatePair: "FFFF:DE04") {
} else {
// Get the error
println(SwiftySurrogate.lastError())
// print -> Optional(Error Domain=SwiftySurrogateErrorDomain Code=504 "High Surrogates (FFFF) must be less than 0xDBFF"
}
iOS 8.0
zh-wang, [email protected]
SwiftySurrogate 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。