Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C protocol static method?

Tags:

I have a protocol in Objective-C, something like this:

@protocol Handler
+(NSString*) getValue;
@end

So now say I have an instance that inherits this protocol and I want to call this method:

[handlerInstance getValue];

This gives a warning because the getValue method is not an instance method. How can I properly call this method from my instance? (Without knowing the concrete class)? I'm guessing something like this, but I'm not exactly sure:

[[handlerInstance class] getValue];
like image 900
Kyle Avatar asked Mar 06 '10 18:03

Kyle


People also ask

What is static method in Objective C?

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.

What is Objective C protocol?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.


1 Answers

[[handlerInstance class] getValue];

Yes, like this.

Unlike Java and C++, class methods can only be sent to the class.

like image 60
kennytm Avatar answered Sep 20 '22 16:09

kennytm