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);