Compile warning during missing categories method implementation

In our Xcode project, we have several targets that share a common code. Each goal includes only those sources that are actually used by it. Therefore, when we use some category methods within classes that are split between targets, we need to make sure that this category implementation is also included in all targets. Xcode doesn't show any warnings at compile time or link time if we forget to include the category implementation in some of the targets. And it's troublesome to do it manually.

Is there any automated way to ensure that category implementations are included in the targets that use them?

+3


source to share


2 answers


Categories are not automatically associated with the final binary.

They are linked if the linker finds the file in which they are defined is in use (which was the source of a persistent error several times ago).

What you can do is use a special flag in the linker: '-all_load' and '-ObjC' in Build Settings / Linking / Other Linker checkboxes

-ObjC Loads all static archive library members that implement the Objective-C class or category.

And from this discussion :

-all_load and -force_load tell the linker to link to the entire static archive in the final executable, even if the linker thinks parts of the archive are not being used.



Another way I use to force the link is to set the C function to a file:

void _linkWithNBLogClass(void)
{
    NSLog(@"%s", __FUNCTION__);
}

      

and call it at the beginning of my application:

 linkWithNBLogClass();

      

This way, thanks to the console feedback, I am confident that my module is loaded and ready to use.

+2


source


The behavior described is as intended and existing code will break if changed.

Before formal protocols, it was necessary to declare methods without defining them. This was for optional methods i.e. That is, to declare an API delegate. The usual technique has been to declare a so-called informal protocol, consisting of a category NSObject

that is never implemented.



But if you have a category implementation, of course its completeness is checked on the category interface. (Otherwise, you will get a "Method definition for X not found" error.) So you don't have a missing method in the category implementation, but you don't have a category implementation.

I don't think this is a big deal. You will get a runtime error instead of a compile time error and just add the category implementation to the target.

+1


source







All Articles