Entering methods into separate files

I have a class ( MyClass) with a lot of methods. Consequently, the .m file has become quite difficult to read. I am relatively new to Objective-C (came from REALbasic), and I was wondering if it is possible to put some of the methods MyClassin different files and then include them in the class. How do I do this in Xcode?

+3
source share
3 answers

Yes, it is possible and, fortunately, this can easily be done in Objective-C with the Category .


Say you have a base class MyClass.

@interface MyClass : NSObject
-(void) methodA;
@end

And the corresponding implementation file (not relevant here).

, :

// the category name is in parenthesis, can be anything but must be unique
@interface MyClass (extended) 
-(void) methodB;
@end

:

@implementation MyClass (extended)
-(void) methodB {

}
@end

- ClassToAddMethodsTo + CatgoryName, :

MyClass+extended.h
MyClass+extended.m

.

+9

Objective-c categories '- , . - - SuperClasses SubClasses.

, , . antipattern

+5

, ..........
, .:)

, , . :

• Class.h
• Class.m
• Class_otherMethod.m



Class.h , . , , "" .h.

@interface Class : NSObject

- (void) method;
- (void) otherMethod;

@end


Class.m #include Class_otherMethod.m Class @implementation :

#import "Class.h"

@implementation Class

- (void) method {

    // do something.
}


#include "Class_otherMethod.m"

@end


Class_otherMethod.m otherMethod:

- (void) otherMethod {

    // do something different.
}




. "" Class_otherMethod.m Class.m, .: P


0
source

Source: https://habr.com/ru/post/1747005/


All Articles