views:

274

answers:

2

Hey Guys,

I have a library written in C that I would like to use in an Objective-C app, either on the Mac or the iPhone.

Unfortunately, since this library is being written by individuals in the open source space, the documentation is quite sparse and incomplete. While I can figure out how to use the stuff in the library, I don't really have an overview of the entire code base.

What I would like to do is wrap the library up into some easily usable and transferrable classes in Objective-C.

  • Does anyone have any tips on how to approach this?
  • Any advice on the best way to get a visual hierarchy of how the library is structured?
  • How would I go about deciding how to best structure the wrapper for reusability and ease of use?

Any and all help will be greatly appreciated, thanks!

+4  A: 

I've done this a few times myself. This can be fun -- it's your chance to fix (or at least hide) bad code!

You can use Doxygen to get a visual hierarchy of the code (although I've only used it for C++ libraries, it also works with C), or any of the other free tools out there.

Don't structure your wrapper class like the underlying library if the library isn't designed or documented well. This is your chance to consider the point of view of the user and how they are going to be using the code. Write your test cases first to figure that out, and/or talk to some people who use the library already.

Two nice design patterns that match up with what you're doing are Adapter and Facade.

Shaggy Frog
+1. You should define some nice interfaces that do what you want first, then fill out the implementations using the C library
Tom Dalling
When you say interfaces, you mean user interfaces, or code interfaces?
Jasarien
I think he means code interfaces.
Jonathan Sterling
+1  A: 

First, remember: a C library is an Objective-C library. You don't actually need to do any wrapping at all, although you may want to if the library interface is especially cumbersome.

Second, if you decide that you want to write a library wrapper, keep it simple. Identify the core functions of the library that you actually plan to use, and think about how best to provide an interface to those functions and those functions only, with your intended usage in mind. Design an interface that you want to work with, then implement it over the library.

Stephen Canon