Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

The closest thing in objective-c is categories.

This is also a good tutorial on categories.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top