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;
– Celso Melero
I supplemented my answer with this information.
– Paulo Rodrigues
Very good. Thank you for your attention.
– Celso Melero
If you have answered your question, consider dialing as an accepted answer, so help others with this same question.
– Paulo Rodrigues