Run your code snippet from Xcode without building the whole project
Update: This plugin certainly can not and should not replace your unit tests, at all. The suitable case is to test and prove some tiny doubts as I mentioned in this blog post.
Have you ever wanted to test out a small snippet of code in Xcode without executing the whole project?
Now with VWInstantRun, you won’t run into this situation any more. An Xcode plugin let you build & run your selected lines of code in Xcode without executing the whole project, you’ll have the output instantly in your Xcode console.
Where the idea come from ?
It comes to me and my colleagues severals times, when we want to test a tiny doubt about an API or some piece of code, we have to build & run the whole project or create another new playground/project to test it. It’s quite annoying.
If you want me to give you some examples, here are a couple of my own use cases:
- Once I try to prove that [[NSURL alloc] initWithString:nil] will cause a runtime crash but [NSURL URLWithString:nil] won’t.
- Once I learned that `flatmap` introduced in Swift 2 can also be used to remove nil during mapping.
- Quicklook different directories found by `NSSearchPathForDirectoriesInDomain`
- X times I’m not sure which one to use, `OrderedAscending` or `OrderedDescending` when I want to sort an array using `NSComparator`.😂 I have to actually run it in an Unit Test to be sure…
The basic implementation behind is quite simple: when you select a couple of lines in Xcode editor, the plugin will copy those lines and put them into a temporary swift file, then build only this file using `swiftc` command. If there is an compiler error, plugin will print standard error output into Xcode console view directly. Otherwise plugin run the binary file then print output into Xcode console view.
Some technical details:
- Plugin use `NSTask` to run the shell command.
- Plugin use `NSPipe` to retrieve standard output and standard error.
- When dealing with Xcode’s private API in Swift, KVC and `performSelector` helps a lot😉
- Great Xcode plugin template makes creating an Xcode plugin way more easier than before, thanks a lot.
The purpose of this plugin is to run your code snippet instantly without building the whole project, so obviously it do have some limitations.
- The code selected should be isolated completely from other contexts (such as instance property, self, or the other function, etc…), otherwise you will only have some compiler errors when you run that code snippet.
- Usually you need to add some stub values as input and add `print()` phrase to actually view your output in console view.
- For now, it only support Foundation module.
Some handy features we may need in the future
- Support also Objective-C code. Run Objective-C code in command line is a little bit different, it’s more verbose than Swift. But it should not be the big problem and will be implemented soon.
- It only support Foundation module for now, theoretically there are more standard modules could be imported automatically.
- Sometimes before we run a function, we may want to be able to input the function’s parameters.
This is my first Xcode plugin. There was a lot of fun when developing and debugging Xcode feature in Xcode. Doing it using Swift was also super interesting and challenging. Thanks a lot to Krzysztof Zabłocki and his plugin KZLinkedConsole, really inspired me a lot about how to deal with private API using Swift. I think this first move is not so bad so far. I may continue once I have another idea.