views:

36

answers:

1

Is it was possible to add objects/classes to a static library in a way that would let them be excluded when the library is weak linked? I tried adding attributes to my obj c classes that tag them as "weak_import" but the compiler says it is undefined.

+3  A: 

Yes, it is possible. Unfortunately, while the runtime and linker support it, the compiler does not, which means you need to declare the assembly stubs for the classes in the headers. In particular, if you wanted to make MyClass weak you would do this in MyClass.h:

asm(".weak_reference _OBJC_CLASS_$_MyClass");
asm(".weak_reference _OBJC_METACLASS_$_MyClass");

@interface MyClass

@end

This will only work on iOS 3.1 and later. For more details read this blog post.

Louis Gerbarg
Thanks, makes sense and is kinda confusing the compiler doesn't support this.
Sam R
Yeah, it is a new feature, that needs to be supported in old OS releases before it is useful, so they prioritized getting the runtime support going as quickly as possible, even though the tool support is incomplete. I expect that the compilers and framework headers will all be updated by the next major releases of Mac OS X and iOS.
Louis Gerbarg