Xcode function library (Objective-C)

Asked

Viewed 265 times

0

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

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

How the class should be implemented to meet these criteria?

Thank you.

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 signs + and -.

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

Can be used this way:

[MyClass classMethod];

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

Further information can be found in object orientation studies.

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

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

And the use:

int res = calcular(2, 2);
  • Paulo, thanks for the feedback. Do you have any way of not having to reference the class? For example: classMethod;

  • I supplemented my answer with this information.

  • Very good. Thank you for your attention.

  • If you have answered your question, consider dialing as an accepted answer, so help others with this same question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.