Playgrounds are Cool!

By kenechi, November 1, 2016

I have an enum with several different cases, each case with its own computed String variable for its title. Elsewhere in my app I needed all of these titles, but I didn’t want to go through and type each one out. This is the kind of thing that should be automated, but as far as I know you can’t automate writing of code in Xcode. Here’s where playground comes in. My enumeration cases have Ints as associated values. I wrote a short algorithm to get the titles of each enum case and add them to an array.

Then I simply copied this array back into my code and finished by adding an assertion that attempting to initialize this enum type with the count of the array should always give nil. This is great because it means that the array doesn’t have to be calculated on the fly. The only pitfall, is that if the order changes then the array would be wrong but for my purposes it’s irrelevant and there are many ways around this. The takeaway is that playgrounds can be used for automating the writing of code that would otherwise be tedious.

What do you think?

Leave a Reply