IPhone - common methods in a separate file

I have several methods that are used by several classes. My idea was to put these methods in a common separate file, and this can be seen by other classes.

How to do it in Xcode?

Sorry, but I'm new to iPhone development.

thank

+3
source share
4 answers

I sometimes have a class Utilityfor which I only define class methods for it. Anywhere I need to use one of these random methods, I just #import "Utility.h"and[Utility doSomeHandyThing]

You define a method with this declaration:

+(BOOL)doSomeHandyThing;

+ , , , - .

+2

. , , , . , , . ; , .

--, .

- (, Squeegy ).

, , , , . :

BaseClass.h:
@interface BaseClass
{
};
- someNiftyMethodWith:(NSString *)someCoolParameter;
@end

BaseClass.m:
@implementation BaseClass
- someNiftyMethodWith:(id)someCoolParameter
{
    NSLog(@"yoy, that was easy to print %@ into the console", someCoolParamater);
}
@end

ExtendingClass.h
@interface ExtendingClass : BaseClass
{
};
- someMethodThatNeedsHelp;
@end

ExtendingClass.m
@implementation ExtendingClass
- someMethodThatNeedsHelp
{
    [self someNiftyMethodWith:@"called by the extending class"];
}
@end

, Objective C. , , .

, : C-, :

Helper.h:
void OldSchoolHelperFunction(void) { printf("simple helper function"); };

Helper.h ,

#import "Helper.h"

@implemenation FooBar
- someMethodThatNeedsHelp
{
    OldSchoolHelperFunction();
}

@end
+1

, . .

0

? , , .

0

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


All Articles