1

I'm just learning objective-c after a fair amount of experience with C#. One of the things I sorely miss is the ability to write extension methods in a separate project that I could reference in all of my projects. Here's some naive c#:

public static bool IsShortString(this string s) {
    return s.length <= 3;
}

In Visual Studio, I could just add a reference, an using, and bam myString.IsShortString() would be a, rather useless, method.

So I think I want to write a static library, but I'm not sure where I'm going from there.

One additional question, if I do write this static library, will I be able to use all of the methods throughout various files in the library using one #import directive, or will I have to import each header individually?

H H
  • 263,252
  • 30
  • 330
  • 514
Tin Can
  • 2,540
  • 2
  • 29
  • 39

2 Answers2

1

The closest thing in objective-c is categories.

This is also a good tutorial on categories.

Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
1

What you are looking for is called Category, and it allows you to add some additional methods to existing classes. Check the reference http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

You can create your own toolkit which is a static library containing categories you made. Common practice is to create one header file containing imports for all the headers in the lib, so when using it, you just do

#import "libName.h"

Also, when creating a static library containing categories it is important to include -all_load and -ObjC linker flags to your project.

Michał Zygar
  • 4,052
  • 1
  • 23
  • 36