Function library in Xcode (Objective-C)

0

I need to create a function library in Objective-C .

The idea is to create something more or less like the Math.h function library. That is, you do not have to instantiate the class to use the methods declared in that class (or library).

How should the class be implemented to meet these criteria?

Thank you.

    
asked by anonymous 31.03.2015 / 20:16

1 answer

1

In Objective-C we have two types: instance methods and class methods . With these methods, your header file ( .h ) looks something like this:

@interface MyClass : NSObject

- (void)instanceMethod;
+ (void)classMethod;

@end

Note the + and - signs.

In a nutshell, what we call instance method needs to be run from the instance of a class you created. On the other hand, what we call class method (in other languages this may be called a static method ) does not have to instantiate the class to execute this method.

Can be used this way:

[MyClass classMethod];

MyClass *object = [[MyClass alloc] init];
[object instanceMethod];

In object-oriented studies, more complementary information can be found.

On the other hand, being Objective-C a "derivation" of C , you can simply declare your method as below, and only need to import where to use:

int calcular(int foo, int bar) {
    return foo + bar;
}

And the use:

int res = calcular(2, 2);
    
31.03.2015 / 20:36