Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I call +class methods in Objective C without referencing the class?
    text
    copied!<p>I have a series of "policy" objects which I thought would be convenient to implement as class methods on a set of policy classes. I have specified a protocol for this, and created classes to conform to (just one shown below)</p> <pre><code>@protocol Counter +(NSInteger) countFor: (Model *)model; @end @interface CurrentListCounter : NSObject &lt;Counter&gt; +(NSInteger) countFor: (Model *)model; @end </code></pre> <p>I then have an array of the classes that conform to this protocol (like CurrentListCounter does)</p> <pre><code>+(NSArray *) availableCounters { return [[[NSArray alloc] initWithObjects: [CurrentListCounter class], [AllListsCounter class], nil] autorelease]; } </code></pre> <p>Notice how I am using the classes like objects (and this might be my problem - in Smalltalk classes are objects like everything else - I'm not sure if they are in Objective-C?)</p> <p>My exact problem is when I want to call the method when I take one of the policy objects out of the array:</p> <pre><code>id&lt;Counter&gt; counter = [[MyModel availableCounters] objectAtIndex: self.index]; return [counter countFor: self]; </code></pre> <p>I get a warning on the return statement - it says -countFor: not found in protocol (so its assuming its an instance method where I want to call a class method). However as the objects in my array are instances of class, they are now like instance methods (or conceptually they should be).</p> <p>Is there a magic way to call class methods? Or is this just a bad idea and I should just create instances of my policy objects (and not use class methods)?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload