Building your own frameworks library

Rob Deans
4 min readOct 21, 2016

--

Frameworks are an incredibly useful way to import frequently used methods, as well as share code and collaborate with other developers!

What is a framework?

A framework is a type of module: a compiled collection of code that is built and shipped together.

Say you have a handful of methods that you often call upon across a variety of projects. You could copy and paste these methods from one sheet to another, but digging through folders of projects to find lines of code can get mighty cumbersome.

Instead you can compile your most frequently used methods into a single .swift file and import it as you would any other framework, such as UIKit or Foundation.

Xcode comes with a variety of extensions built in such as .count and .sorted, however for this example we are going to use a .shuffle() method that can be used on type Array. It will “randomly” shuffle the values within the Array using the swap function, and can be applied to [Int], [String], and any kind of array!

Be sure to declare your extension as a “public extension” so that it can be readily accessed when it is called upon!

Now that we have a .swift file of our favorite methods saved, we can begin to build our framework.

Building a Framework

In Xcode you can build a Framework just like any other project, but this time instead of selecting Single-View Application, scroll down and select Cocoa Touch Framework.

Now add your source file (the .swift file you just created) into the framework’s folder either by dragging it into the project or clicking the + on the bottom-left of Xcode. The navigator should now look similar to the one below:

Be sure your file is properly nested!

Great! Once that’s been accomplished you can close your framework and create a new Xcode project (or open any existing project). Using the exact same method as above, add your framework to the Xcode project by locating the framework’s .xcodeproject file and dragging or adding it to your Xcode project. Again, make sure it’s properly nested!

Your framework is now part of your project, but they aren’t really communicating yet. To solve that, open your framework’s Products folder. Next click on the top item in the navigation bar (your project’s name) and on the corresponding page scroll down to Embedded Binaries. There, click and drag your framework’s .framework from the Products folder into the Embedded Binaries.

This next part is a bit esoteric, however I find it makes the import run more smoothly. Click on the Build Settings of the same screen and scroll down until you find Search Paths. Under both Framework Search Paths and Header Search Paths, add $(SRCROOT) and make it recursive. After this is set, it’s time to BUILD! CMD + B

Build time <= Eiffel Tower build time

Ok great! So what do we do now… Did it work? Let’s find out!

Go to your ViewController (or any file you will be working on). At the top, try importing your framework simply by entering “import” and your framework’s name (ideally it should show up automatically).

And… voilà! Any type of extension you had in your framework should now apply to that value in your project. But oh no… did you forget to add method? Fear not! You can close the project and update your framework any time, and the new framework will appear up-to-date in your project!

That’s all for now, I hope this simple and brief tutorial will help make all of your coding lives lazier, err, easier and more productive!

--

--

Rob Deans

Chronicling curiosities in Swift and Beyond ~ iOS Developer // Tech Talent Pipeline // Flatiron School Alumnus // Musician // NYC