Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

xcode - How to list (almost) all emojis in Swift for iOS 8 without using any form of lookup tables?

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.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can loop over those hex values with a Range: 0x1F601...0x1F64F and then create the Strings using a UnicodeScalar:

for i in 0x1F601...0x1F64F {
    guard let scalar = UnicodeScalar(i) else { continue }
    let c = String(scalar)
    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 {
        guard let scalar = UnicodeScalar(i) else { continue }
        let c = String(scalar)
        print(c)
    }
}

For those asking, the full list of available emojis can be found here: https://www.unicode.org/emoji/charts/full-emoji-list.html

A parsable list of unicode sequences for all emojis can be found in the emoji-sequences.txt file under the directory for the version you're interested in here: http://unicode.org/Public/emoji/

As of 9/15/2021 the latest version of the emoji standard available on Apple devices is 13.1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...