ProRobot

Selected Article

How to Determine If a String is "One of Three" in Swift

When building apps, you might come across interesting ways to map input data to specific outputs. One fun example is taking an input string and categorizing it into one of three distinct emoji characters: 🙈 (see no evil), 🙊 (speak no evil), or 🙉 (hear no evil).

In this post, I'll walk you through a Swift function that computes the Unicode sum of a string and then returns one of the three emojis based on the sum. Let's dive in!

The Goal: A Function That Returns One of Three Emojis

We want a function that:

  1. Takes a string as input.
  2. Computes the sum of the Unicode values of the string's characters.
  3. Uses the remainder of that sum divided by 3 to return one of three emojis:
    • 🙈 (see no evil)
    • 🙊 (speak no evil)
    • 🙉 (hear no evil)

Here's the Swift Function

func isStringOneOfThree(_ input: String) -> String {
    let sum = input.unicodeScalars.map { Int($0.value) }.reduce(0, +)
    
    // Use modulo 3 to cycle through the three emoji
    let remainder = sum % 3
    
    switch remainder {
    case 0:
        return "🙈"  // See no evil
    case 1:
        return "🙊"  // Speak no evil
    case 2:
        return "🙉"  // Hear no evil
    default:
        return "" // This will never be reached, but Swift requires a default case
    }
}

How It Works

This function processes the input string in three main steps:

  1. Calculate the Unicode Sum: The function maps each character in the string to its Unicode scalar value, converts that to an integer, and sums up all the values. Here's how this part works:

    input.unicodeScalars.map { Int($0.value) }.reduce(0, +)
    

    For example, for the string "abc", the Unicode values are 97, 98, and 99. The sum is 97 + 98 + 99 = 294.

  2. Compute the Remainder: We calculate the remainder when dividing the sum by 3. This gives us a number from 0 to 2.

  3. Return One of Three Emojis: Based on the remainder, the function returns one of the three emojis:

    • 0 returns 🙈 ("See no evil")
    • 1 returns 🙊 ("Speak no evil")
    • 2 returns 🙉 ("Hear no evil")

Example Usage

Here are a few examples of how this function works with different strings:

print(isStringOneOfThree("hello")) // 🙉
print(isStringOneOfThree("swift")) // 🙈
print(isStringOneOfThree("programming")) // 🙊

In each case, the string gets categorized into one of the three emojis, based on the sum of the Unicode values of its characters.

Why Is This Useful?

While this particular function is a fun example, the concept of using modulo (%) to group or categorize data can be very useful in real-world scenarios. Whether you're dividing data into batches, evenly distributing load, or, like here, categorizing input into specific groups, this pattern is widely applicable.

Conclusion

Swift gives us powerful tools to process strings and work with Unicode values. With just a few lines of code, we can map a string to one of three predefined outputs. Whether you're creating a fun feature in your app or learning more about how to work with strings, this approach is a great example of how simple logic can be both efficient and fun.

Give it a try and let us know what kind of fun applications you can think of for this concept!

profile photo of Konstantin Yurchenko, Jr.

Konstantin Yurchenko, Jr.

Last edit
7 days ago
Published on