D
D
deleted-mezhevikin2013-09-26 17:57:07
Objective-C
deleted-mezhevikin, 2013-09-26 17:57:07

Breaking down a large class into categories in objective-c?

There is a PlayerViewController class with a large number of methods, I want to break it into categories, but I don’t understand how to do it so that by connecting PlayerViewController.h methods from all its categories are available.
As tried:
I create category Timer.

//
// PlayerViewController+Timer.h
//

@interface PlayerViewController (Timer)

- (void)testMethod;

@end

//
//PlayerViewController+Timer.m
//

#import "PlayerViewController.h"
#import "PlayerViewController+Timer.h"

@implementation PlayerViewController (Timer)

- (void) testMethod
{
    NSLog(@"test");
}

@end

In the main class I am now trying to call the testMethod method from the category:
//
//  PlayerViewController.h
//

#import <Foundation/Foundation.h>
#import "PlayerViewController+Timer.h"

@interface PlayerViewController : UIViewController
    
@end

//
//  PlayerViewController.m
//

#import "PlayerViewController.h"

@implementation PlayerViewController

- (void)viewDidLoad
{
    [self testMethod];
}

@end

Errors:
/Users/user/Desktop/Projects/SplitClass/PlayerViewController+Timer.h:5:12: Cannot define category for undefined class 'PlayerViewController'
/Users/user/Desktop/Projects/SplitClass/SplitClass/PlayerViewController.m:11:11: No visible @interface for 'PlayerViewController' declares the selector 'testMethod'

Edit1: End example solution
github.com/nullproduction/SplitClass

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
silvansky, 2013-09-26
@deleted-mezhevikin

The first error is that the PlayerViewController is not defined at the time the category is declared. The second follows from the first - the category is not declared, which means there is no method.
If you really want to import just one header for all categories, then my solution is this:
File PlayerViewControllerMain.h : main declarations (without categories)
PlayerViewController + {Category1|Category2|...} .h files : category declarations, each imports PlayerViewControllerMain. h
File PlayerViewController.h : includes all of the above files for easy access to all categories at once

D
deleted-mezhevikin, 2013-09-26
@deleted-mezhevikin

Files PlayerViewController+{Category1|Category2|...}.h: category declarations, each import PlayerViewControllerMain.h

And do we do categories for PlayerViewControllerMain?
It seems that I did everything as you said, it did not help.
The code for this test project is www.dropbox.com/s/x7wiwvababgcho2/SplitClass.zip

A
Alexey Storozhev, 2013-10-04
@storoj

IMHO it is still useful to try AppCode (http://www.jetbrains.com/objc/), it justifies its cost by 146%. For starters, you can try EAP, and there it won’t be long before the sale.
A separate plus can be considered the uniformity of this ide for different platforms - java, php, objc, html / css - almost the same hotkeys and appearance are everywhere, for me it's important.
If you dig a little deeper and study its capabilities, then it can help a lot and “write” (generate) a lot of code for the developer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question