How can I make a static method in Objective-C?

In Java, I may have a class, for example, Utility and I have a static method called changeToCapitalLetter, so I can do something like this:

Utility.changeToCapitalLetter(myString);

How can I do the similar thing in Objective C?

Thanks a lot


In Objective-C, you call this "class methods", see here:

@interface MyClass : NSObject

+ (void)aClassMethod;
- (void)anInstanceMethod;

@end

The + is the important thing; you call the method like this: [MyClass aClassMethod];


From Wikipedia: Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance.

This describes exactly what Objective-C's class methods are not.

An Objective-C class method very much requires an instance that is the target of the method invocation. That is, it requires an instance of the metaclass that describes the class object being invoked.

Unlike static methods, Objective-C's class methods can be inherited (which, in combination with having the aforementioned self, is exactly why many classes can share a single, simple, implementation of +alloc on NSObject without needing their own custom implementations) and invoking a class method goes through the exact same objc_msgSend* based dispatch mechanism as any other method call site.

Objective-C's class methods can be overridden across the class hierarchy and they can be swizzled. None of which is supported in languages that typically offer static methods in lieu of class methods.

Though class methods and static method are in practice the same most of the time, they are different. With static methods the the class is acting as a namespace qualifier. With class methods the class itself is an object and so class methods are to the class object exactly the same thing instance methods are to an instance, as a consequence you can do the following

@interface TestClass : NSObject

+(void)classOrInstanceMethod;
-(void)classOrInstanceMethod;

@end

@implementation TestClass

+(void)classOrInstanceMethod{
    NSLog(@"%s", __PRETTY_FUNCTION__); 
}
-(void)classOrInstanceMethod{
    NSLog(@"%s", __PRETTY_FUNCTION__); 
}
@end

int main(int argc, const char* argv[])
{
    @autoreleasepool{
        NSLog(@"Static method in ObjectiveC");
    }

    NSArray* arr = [NSArray arrayWithObjects:[[TestClass alloc]init],
                                        [TestClass class], nil];
    for(id obj in arr)
        [obj classOrInstanceMethod];
}

which version of classOrInstanceMethod is called depends on whether obj is a class object or and instance. If you are familiar with the factory class pattern, this pattern is part of the Objective-C language.

The bottom line is that static methods and class methods are very different. While that difference is mostly transparent for day to day coding purposes, there are still situations where knowing how class methods work can save you a ton of unnecessary lines of code.

Original Links here and here