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];
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.
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.
[[handlerInstance class] getValue];
Yes, like this.
Unlike Java and C++, class methods can only be sent to the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With