1

enter image description here

If you launch TextEdit and choose Emoji & Symbols under Edit, you will have a list of 291 symbols for Smileys and People under Sierra (macOS 10.12). And I want to list all these 291 emoji symbols with NSCollectionView. Now, I have a string consisting of 291 emoji symbols. And I separate them to create an array as follows.

class SmileyViewController: NSViewController, NSCollectionViewDelegate, NSCollectionViewDataSource {
    // MARK: - Variables
    var emojiArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let emojiString = "..." // 291 symbols
        emojiArray = emojiString.characters.map { String($0) }
        print(emojiArray.count) // 408, not 291
        collectionView.reloadData()
    }
}

Running the code above, the array that I get actually contains 408 symbols. If I take a close look, some of them separate themselves into three symbols. For example, a symbol named man with blonde hair turns itself into three symbols. One of them is a gender symbol. this topic suggests that a single emoji symbol may be a combination of a few or more symbols. Is there something that I can do to separate this particular string containing 291 symbols into an array containing 291 symbols? Thanks.

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
El Tomato
  • 6,479
  • 6
  • 46
  • 75

2 Answers2

1

A better way to create a collection view with emoji are with their unicode characters. https://apps.timwhitlock.info/emoji/tables/unicode lists the emoji that are available. One implementation I have used is my answer here: Emoji Picker Ios Swift

Ali
  • 1,002
  • 1
  • 11
  • 21
1

This behavior is actually expected. In swift 3.0, emoji is not counted as grapheme cluster. Many emoji are actually a sequence of single emoji connected by zero with joiner. So the character property is actually giving you all the single emoji. That's why you have 408 single emoji from 291 top level emoji

This behavior of emoji string get improved a lot in swift 4. In swift 4, you can directly call count to your emoji string and you will get exactly 291 as result

For further information about this topic, have a look here

Fangming
  • 24,551
  • 6
  • 100
  • 90