I’ve implemented a tabbed UITableViewController. There are some tabs on the top which reload the contents of the table. Based on the selected tab different cells are shown to the user. It all works nice, but I end up with a source file which contains 3 different implementations, and gets a little bit bulky and confusing, even using pragmas to mark sections of the source code.
I’ve thought of creating selectors at runtime from strings based on the selected tab, then splitting the .m file into several putting there the renamed methods, but then there’s the forced @end and the end of a file and the compiler telling you that there are missing methods to be implemented.
Really, it looks like objective-c wasn’t designed to split the source though several files. Is there any design pattern that can be used for this? Somehow I managed to emulate all this using #include <otherfile.m> before the @end of the main class, but it doesn’t look pretty. Also, Xcode gets confused as hell if I try to include that file into the project, since it tries to compile it separately (at least I can include the files in the project and disable their inclusion in the target).
In the end I have resorted to the basic preprocessor, meaning includes and some trickery. Here’s what I’ve done:
filename.m, create onefilename_functionality.mfile for each tab/group of related functionality and put there the methods. Note that there is no@implementation,@end,#include, etc, in this file, just the methods.filename.mand just before your@endmarker, add the necessary#include "filename_functionality.m", which effectively tells the preprocessor to treat all the files as a single one.At this point, it works! However, Xcode is still really annoying, despite being able to open the subordinate source code files, it doesn’t parse them properly, so you can’t use the quick navigation bar to jump between methods, for example. Again, to solve this more preprocessor trickery:
At the beginning of each subordinate file, add (yuck, wiki formatting is broken):
#ifndef _INCLUDE_FILES@implementation BIEntity_view_controller#endifAt the end of your subordinate file repeat with
ifndefenclosing an@end._INCLUDE_FILES.What this does is trick XCode into thinking that this is a proper implementation file, so syntax highlighting, completion and navigation bars work as expected.
The only minor nit with this setup is that Xcode doesn’t properly report errors for lines in your subordinate files, it just points to the include and stays there. Now I have to right click on the line error and select Reveal in Log which sows the full console message where the correct line numbers are.
This is not a big problem if you don’t write bad code (hehe) or use an external text editor anyway, but will seriously hurt if you are used to the quick fix keys to jump from one error line to another.
With this trick I’ve split a 1151 line file into four of sizes 558, 342, 55 and 145 lines each. Now the functionality is better related and the code can still work as part of the same class, so I don’t need to write accessors as would be the case if using a language construct like classes or interfaces.