[Swift] Quick number to word conversion

If you’re ever setting content for things using loops (who isn’t, right?), you might often use the loop variable in a string to label something. Item 1, Item 2, etc. Especially when you’re simulating or prototyping something that will consume actual live data later on.

A nice touch is to get rid of those numerals in your labels or other text controls. They look so… engineer. Meaning more function over form. I like both.

Slap this into your loop and set your labels with more care.

let formatter = NSNumberFormatter()
formatter.numberStyle = .SpellOutStyle
let numberString = formatter.stringFromNumber(y+1)
let nString = numberString?.capitalizedString
myDescription.text = "Collection \(nString!)"

Such a tiny bit of code and it really does dress things up nicely. Some might even think that you’ve set up an array of String values and populated using that.

Crazy. As in crazy good.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.