2014-12-03 217 views

回答

8

这是一个“纯粹的雨燕”的方法,不使用粉底:

let smiley = "" 

let uni = smiley.unicodeScalars // Unicode scalar values of the string 
let unicode = uni[uni.startIndex].value // First element as an UInt32 

print(String(unicode, radix: 16, uppercase: true)) 
// Output: 1F60A 

。请注意,斯威夫特Character代表一个“Unicode字形集群” (比较Swift博客中的Strings in Swift 2),其中 由几个“Unicode标量值”组成。以从下面@ TomSawyer的评论的例子 :

let zero = "0️⃣" 

let uni = zero.unicodeScalars // Unicode scalar values of the string 
let unicodes = uni.map { $0.value } 

print(unicodes.map { String($0, radix: 16, uppercase: true) }) 
// Output: ["30", "FE0F", "20E3"] 
+0

不与组合的表情符号像0️⃣工作,❤️ – TomSawyer 2015-12-11 20:05:14

+0

@TomSawyer:'0️⃣'包括三个Unicode代码点:U + 0030(字符“ 0“),然后是U + FE0F(VARIATION SELECTOR-16)和U + 20E3(COBINING ENCLOSING KEYCAP) - 你认为正确的输出应该是什么? – 2015-12-11 20:16:23

+0

@TomSawyer:'❤️'甚至不是单个字符,它是由字符'“,”❤️“,”“,”“'组成的* String *。 – 2015-12-11 20:44:59

0

它的工作原理类似,但付出,当你在打印注意:

import Foundation 

var smiley = "" 
var data: NSData = smiley.dataUsingEncoding(NSUTF32LittleEndianStringEncoding, allowLossyConversion: false)! 
var unicode:UInt32 = UInt32() 
data.getBytes(&unicode) 
// println(unicode) // Prints the decimal value 
println(NSString(format:"%2X", unicode)) // Print the hex value of the smiley 
相关问题