Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Static Libraries

Static Libraries

A look at how I've been using Static Libraries in the Cocoa/Cocoa Touch code that I've been playing with in my spare time.

Matt Diephouse

September 11, 2012
Tweet

More Decks by Matt Diephouse

Other Decks in Programming

Transcript

  1. Code that has been compiled and can be linked into

    other code. What is a Static Library? 2
  2. To share code between projects Why use a Static Library?

    6 You can share between iOS and Mac versions of the same app or share between different apps.
  3. To share code between projects Why use a Static Library?

    6 On the Mac, you can use Frameworks. But they aren’t allowed on iOS, so use Static Libraries to reuse code. You can share between iOS and Mac versions of the same app or share between different apps.
  4. To better structure your code Why use a Static Library?

    MVC is a fine place to start, but for more complex projects, additional layers can be helpful.
  5. To better structure your code Why use a Static Library?

    Separate your concerns and keep them separate. Different targets and class prefixes can enforce your architecture. MVC is a fine place to start, but for more complex projects, additional layers can be helpful.
  6. To write testable code Why use a Static Library? Example:

    splitting out the concept of a library in a Core Data app
  7. To write testable code Why use a Static Library? If

    code is written to be reusable between different apps, then it won’t rely on app-specific information. This will make it more testable. Example: splitting out the concept of a library in a Core Data app
  8. Reusable code is better code Why use a Static Library?

    9 Example: splitting out the concept of a library in a Core Data app
  9. Reusable code is better code Why use a Static Library?

    9 This is like designing code by writing the tests for it: designing for reusability informs your design decisions. Example: splitting out the concept of a library in a Core Data app
  10. Your most valuable asset is the code you’ve already written

    Why use a Static Library? Example: Panic Inc—Transmit, Coda, iSSH
  11. Your most valuable asset is the code you’ve already written

    Why use a Static Library? Corollary: write apps that leverage the code you have. Example: Panic Inc—Transmit, Coda, iSSH
  12. Creating a Static Library • Move the App into a

    subdirectory • Create a workspace in the top directory • Add the app project to the workspace • Add a project for the static library (iOS > Framework & Library > Cocoa Touch Static Library) in the new workspace • Link the library into your project • Set up Public Headers Folder Path for the library: public/LibraryName • Set up the Header Search Paths for the app: $(CONFIGURATION_BUILD_DIR)/public • Add the library to the Build phase of the scheme • Test by importing a header • Add any non-source resources to the app project • Add -ObjC to “Other Linker Flags” for the library if you want to use ObjC categories