Tel Tel - 1 year ago 46
Swift Question

How to randomize the case of letters in a string in Swift?

Given a string, I would like to change the case of each letter to a random between upper and lower case.

For example:

let myString = "Have a nice day!"
let newString = "haVe A NiCe dAY!"

Answer Source

I would use arc4random_uniform and map like this:

let myString = "Have a nice day!"

let result = { (char) -> String in
    if arc4random_uniform(2) == 0 {
        return String(char).lowercaseString
    return String(char).uppercaseString


hAvE A NICe DAy!