Search code examples
xcodeswiftframeworksconsole-applicationcode-reuse

In Swift, what are my code reuse options for designing console applications, and how do I implement them?


My current code base involves writing console applications in C++ where I import a static library containing many common classes.

I would like to move to Swift and replicate this process. However, I have come across a couple difficulties:

  • As it stands, one apparently cannot create static libraries in Swift.
  • As it stands, one apparently can not import a framework into a Swift console application.

Currently I have been experimenting with writing a single view application and importing Frameworks that way, but enough has gone wrong that I would really like to simplify that and stick with console applications.

Given that, so far it appears that my only option for code reuse is to keep Swift files in common directories and drag them as needed into the console application. Since these console applications are for my own use and I'm only interested in the data they generate (i.e. I don't actually give the program to a user) this is actually a workable solution. I was hoping, however, there might be something a bit cleaner.

Any other suggestions on what to do for code reuse for pure Swift console applications, and if so, how do I go about doing it?


Solution

  • You can create a standard Swift framework that contains your common classes, and then create a console application that lives in a bundle. This will allow you to copy the framework into the bundle's framework folder which your console application will have access to at run time.

    You can find more details about this approach in Alsey Coleman Miller's article about using embedded frameworks in OS X command line applications.

    The downside, of course, is that your console application now lives inside a bundle.