views:

3173

answers:

6

I'm currently using a lot of the same subclassed objects with custom methods. It would be more convenient to create my own library which I can use for several projects.

The goal is to have my own classes being available in the same way classes like UIView, CGRect etc are, including convenient methods like CGRectMake, both with classes and structs. To sum it up, I want to create my own equivalents of:

  • Classes like UIView
  • Structs like CGRect
  • Convenient functions like CGRectMake
  • Have this available as a library
  • Have this available as an XCode template, thus, having these custom Objects available as 'new files' in XCode

So basically I'm looking for instructions on how to create classes, structs etc in order to create all the above. What is the best way to do this? The 320 project seems like a good starting point. But it lacks (I think) in:

  • having the library available in new projects right away
  • having the new classes available under 'new file'

Even if I would create an own static library, will I be able to release the app on the app store, since linking to 3rd party libraries is not supported on the phone?

For your convenience, these are basically the sub questions, covering the scope of this question:

  • How can I create my own library for Mac / iPhone development?
  • How do I create classes, structs and inline function for this library?
  • How do I create my own Xcode template based on this library?
  • Will I be able to release iPhone apps using my own static library?
+4  A: 

The 320 project is a good example of an iPhone class library. You basically compile your project down into a .a library and then statically link against this in your client projects.

drewh
I checked this out, but I can't seem to get a grasp on it. Could you elaborate on this?
Kriem
He gives pretty detailed instructions here: http://github.com/joehewitt/three20/tree/masterWhat exactly don't you grasp?
drewh
I didn't quit understand how to do the things I stated in the question. ut it's clear now. Thanks to the community. The instructions are pretty detailed indeed.
Kriem
We just needed the URL to the instructions. drewh provided that. Thanks!
bentford
+4  A: 

If you were doing this for a Mac, you'd create a framework. However, you mention UIView, so obviously you're working with the iPhone. Apple doesn't allow iPhone applications to dynamically link against other libraries at runtime, so your only option is to create a static library. A static library is linked into the application executable when it's built.

To my knowledge, there's no static library project template in Xcode. What you'll likely have to do is start with a different iPhone Xcode template and add a Static Library target. Hang on to the default application target; you can use that to build a simple test application to make sure the library actually works.

To actually use the library in an application, you'll need two things: the compiled library (it has a .a extension) and all the header files. In your finished application, you'll link against your static library, and you'll need to #import the header files so that the compiler understands what classes, functions, etc. are available to it. (A common technique is to create one header file that imports all the others. That way, you only need to import a single file in your source files.)

As for creating your own custom templates, there's a simple tutorial here that should get you started: http://www.macresearch.org/custom_xcode_templates You can probably copy the default templates and just customize them to suit your purposes.

The struct syntax looks like this:

typedef struct _MyPoint {
    CGFloat x;
    CGFloat y;
} MyPoint;

Structs are are declared in header files, so you can (I believe) Command+Double Click on the name of a struct to see how it's declared.

Another little trick for creating structs is to do something like this:

MyPoint aPoint = (MyPoint){ 1.5f, 0.25f };

Because the compiler knows the order of fields in the struct, it can very easily match up the values you provide in the curly braces to the appropriate fields. Of course, it's more convenient to have a function like MyPointMake, so you can write that like this:

MyPoint MyPointMake(CGFloat x, CGFloat y) 
    return (MyPoint){ x, y };
}

Note that this is a function, not a method, so it lives outside of any @interface or @implementation context. Although I don't think the compiler complains if you define it in an @implementation context.

CGPointMake is defined as what's known as an inline function. You can see the definition for that in the Cocoa header files, too. (The difference between an inline function and a normal function is that the compiler can replace a call to CGPointMake with a copy of CGPointMake, which avoids the overhead of making a function call. It's a pretty minor optimization, but for a function that simple, it makes sense.)

Alex
Thanks! This is helping a lot. One question though; Instantiating MyPoint with convenient methods like 'MyPointMake(x,y)' (as 'CGRectMake(x,y,width,height)') is still possible?
Kriem
In C99 (which you can set in the build settings), you can use a compound literal: (MyPoint){ x, y }. That works for Objective-C, too, as long as you set it to C99. If you really want a function, of course it's possible. You'll need to write your own, of course.
Peter Hosey
I see. Thanks Peter!
Kriem
And thanks Alex!
Kriem
+1  A: 

The 320 project seems like a good starting point indeed. But it lacks (I think) in:

  • having the library available in new projects right away
  • having the new classes available under 'new file'

Those are project and file templates. For more information, ask the Google.

Peter Hosey
+1  A: 

If you plan on releasing this on the app store, you wont be able to use your library in the way that you would like. As mentioned above, linking to 3rd party libraries is not supported on the phone. I think there is a 'hack' way to make it work, but you'll lose distribution.

The best I could come up with was putting all the relevant code in a directory and sharing it that way. I know its not as elegant, but its their limitation ie. out of our control.

Brian
But linking to my own static library is supported? Without losing distribution?
Kriem
Uhm, to be sure, so what you'r saying is that although it is possible to create an own static library, you won't be able to release the app on the app store?
Kriem
+2  A: 

Since this is a community wiki now, I thought it will be helpful to link some resources and tutorials:

http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html

http://www.clintharris.net/2009/iphone-app-shared-libraries/

Enjoy!

yn2
+5  A: 

FYI Xcode 3.2 has a new project template called Cocoa Touch Static Library. You might want to go that route.

lottadot