Monday, May 11, 2009

Static Methods

One more tip. If you're looking for how to create static methods that can be called without an instance of the class being initialized, it's pretty simple. (I wish I hadn't spent an hour trying to find it.)

Use the (plus) + in front of your method instead of the (minus) -

Example (assuming your class name is MyClass, and this method is defined within the implementation):

+(MyClass *)getMyClassInstanceOfType:(int) myClassTypeID
{
    MyClass *retMyClass = [[MyClass alloc] init];
    switch(myClassTypeID)
    {
       ...
    }
    return retMyClass;
}


This method can be called in your code without instantiating anything like this (assuming #define kMyClassTypeFoo 1 is in the header):

MyClass *myClass = [MyClass getMyClassInstanceOfType:kMyClassTypeFoo];


Hopefully that's a time-saver for someone.

1 comment:

  1. Hello,
    Useful information, many thanks to the author. It is puzzling to me now, but in general, the usefulness and significance is overwhelming. Very much thanks again and best of luck!
    Hire iphone Developer

    ReplyDelete