If you are ever in the middle of a project and want to write a quick snippet to determine if an API is going to respond the way you think it will then Code Runner can help you out. Quite simply it sets up an environment where you can just start typing in your language of choice and, when you’re ready to test, has a giant play button similar to Xcode that will run your code.
Heres an example from the other day. I wanted to do a check on the iOS system version for a very particular edge case and I was browsing through all the terrible and overly complicated comparison functions proffered in this Stack Overflow Question and came across the option of using the built in
NSNumericSearch option for
compare:options:. I wanted to verify this method would work correctly with different version numbers so I fired up code runner and tried out this small programme with a few different version numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Very quickly I had determined that the option worked as advertised and was able to include it in my code with the relevant test cases.
If you make a typo in Code Runner it can be hard to debug as you dont necessarily get a stack trace or other information on code crash. Luckily Code Runner has the option to add custom running environments to its editor and by duplicating and slightly tweaking the existing objective-c template I was able to make it run the code in the lldb debugger.
When you click the play button it fires up lldb and will load your executable. To run you can type r and hit return. You can also use all the other lldb commands you would expect like add breakpoints and inspect variables at runtime. Its not as nice as the interface in Xcode but the liklihood is that the issues you will encounter wont be as hard to debug!
Classes, Protocols, Categories and more
I’m not sure its widely known that we keep our classes in separate files out of convention rather than necessity but with this knowledge your use of Code Runner can become quite advanced indeed. It can be used to prototype classes and methods on those classes that can quickly be integrated into a larger application. It can even be handy if practising computer science style challenges.
Heres an example where I was practising determining if a string was a permutation of another string. This is more of a trick question as once you realise that all permutations of any particular string are contained in a doubled version of that string the implementation is straightforward. Here I’ve implemented the check in code runner but as a category method on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
So I reccommend you check out Code Runner. Its well worth the money and it can save you time with your development.