3

I have a .m file, containing a @implementation , but it has gotten too big and I'm trying to move some of the method functions to a 2nd file.

Both .m files begin with

@implementation GesticulatorViewController

@synthesize score_display;
@synthesize game_status;
@synthesize player_options;
@synthesize total_players_field;
@synthesize gesticulation_sentence;
@synthesize gesticulation_input;
@synthesize main_view_manager;
@synthesize game_state;

But I'm getting linker warning: "ld: duplicate symbol _OBJC_IVAR_$_GesticulatorViewController.gesticulation_input "

Yusubov
  • 5,815
  • 9
  • 32
  • 69
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • you can not use same class name twice. – rishi Jun 21 '12 at 17:52
  • Duncan and I arrived at the same solution. I provided some example code in this answer -- http://stackoverflow.com/questions/13357775/split-objective-c-code-into-multiple-files/15315895#15315895 – software evolved Mar 09 '13 at 21:49
  • See [Split an implementation file into multiple files in iOS](http://www.fantageek.com/1373/split-an-implementation-file-into-multiple-files-in-ios/) – onmyway133 Oct 09 '14 at 15:34
  • Look at an example for similar case: https://journeytoios.wordpress.com/2016/11/02/split-ios-code-in-different-files/ – NeverHopeless Nov 02 '16 at 22:42

4 Answers4

3

You cannot have the same class implementation in two different files.

In your case, you cannot split the implementation of GesticulatorViewController into two .m files.

EDIT:

I would use Objective-C categories to disperse the implementation.

With categories, you have the opportunity to group together methods that perform similar tasks.

Here's an explanation of "Categories and Extensions" from Apple's documentation: https://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1

Felix Mo
  • 71
  • 4
  • What, then, is the fastest way to split a large class into multiple subfiles? Creating multiple classes costs development time. – Doug Null Jun 21 '12 at 18:49
2

I would recommend making more than one controller. Each controller handles a different piece.

I am making a video app. I have a PlayerControlsViewController, it has all the Play and pause buttons within its view.

I have also a Tools controller which has the selection tools and any menu items within it.

The properties for those controllers take that controllers view and remove from super view. then set the view somewhere within its own view and connects it to the PlayerController. which houses the player within it.

Each of these controllers houses its own code to handle its tasks. And sends messages back to the main view controller via a protocol for each.

This will relay commands back and forth between the other controllers, and maintain the setting on the main view controller.

This I believe is the expected standard of operation for the apps to function correctly, and be easily maintainable.

This method also works with Navigation controllers and Tab controllers. Since they maintain their own code, you can add another view controllers view within your own view.

Just remember to remove it from its superview before adding it to your view.

The Lazy Coder
  • 11,560
  • 4
  • 51
  • 69
1

If it all needs to be in one view, but you think the implementation is too large, you can always do categories :) Just go to File->New->Objective-C Category. Then make the category on your view controller, and add it!

Just remember you cannot add new ivars or properties in the category. You can, however, utilize properties and ivars from your original class.

Also, if you find that you have a LOT of code in your .m view controller files, you might think about looking a little further into the MVC paradigm to split some functionality into other classes. Remember, the view controller should only handle view changes - data manipulation, etc. should be done by other classes :)

anon_dev1234
  • 2,143
  • 1
  • 17
  • 33
1

Another option would be to copy the source code for some of the methods to another .m file, then #include that file in the main .m file. Put the #include at the point where you cut methods out of the main file.

The trick to making this work is to add the #included source file to your project, but un-check all targets. You don't want Xcode to try to compile the file on it's own - only #include it in your main view controller file.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • This is a good temporary fix and worked for me, but it breaks XCode's code navigation tools for the included file (#pragma mark, etc.) – Paul Slocum Dec 29 '13 at 22:24