xcode 如何在不使用任何形式的查找表的情况下在 Swift for iOS 8 中列出(几乎)所有表情符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26170876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list (almost) all emojis in Swift for iOS 8 without using any form of lookup tables?
提问by Yogesh
I'm playing around with emojis in Swift using Xcode playground for some simple iOS8 apps. For this, I want to create something similar to a unicode/emoji map/description.
我正在 Swift 中使用 Xcode playground 来处理一些简单的 iOS8 应用程序的表情符号。为此,我想创建类似于 unicode/emoji 映射/描述的东西。
In order to do this, I need to have a loop that would allow me to print out a list of emojis. I was thinking of something along these lines
为了做到这一点,我需要一个循环来打印表情符号列表。我正在考虑这些方面的事情
for i in 0x1F601 - 0x1F64F {
var hex = String(format:"%2X", i)
println("\u{\(hex)}") //Is there another way to create UTF8 string corresponding to emoji
}
But the println() throws an error
但是 println() 抛出错误
Expected '}'in \u{...} escape sequence.
Is there a simple way to do this which I am missing?
有没有一种简单的方法可以做到这一点,而我却缺少这种方法?
I understand that not all entries will correspond to an emoji. Also, I'm able create a lookup table with reference from http://apps.timwhitlock.info/emoji/tables/unicode, but I would like a lazy/easy method of achieving the same.
我了解并非所有条目都对应于表情符号。此外,我可以参考http://apps.timwhitlock.info/emoji/tables/unicode创建一个查找表,但我想要一种懒惰/简单的方法来实现相同的目标。
回答by Mike S
You can loop over those hex values with a Range
: 0x1F601...0x1F64F
and then create the String
s using a UnicodeScalar
:
你也可以遍历这些十六进制值有Range
:0x1F601...0x1F64F
,然后创建String
S使用UnicodeScalar
:
for i in 0x1F601...0x1F64F {
var c = String(UnicodeScalar(i))
print(c)
}
Outputs:
输出:
If you want all the emoji, just add another loop over an array of ranges:
如果您想要所有表情符号,只需在一系列范围内添加另一个循环:
// NOTE: These ranges are still just a subset of all the emoji characters;
// they seem to be all over the place...
let emojiRanges = [
0x1F601...0x1F64F,
0x2702...0x27B0,
0x1F680...0x1F6C0,
0x1F170...0x1F251
]
for range in emojiRanges {
for i in range {
var c = String(UnicodeScalar(i))
print(c)
}
}