Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Class methods instance methods

ryantcbryantcb Posts: 667Registered Users @ @ @
edited July 2013 in Mac OS X Development
Ok so an instance method is called on an instance of a class and a class method is called on the class. But why? when to make a class method and when an instance method? For example NSDate has a class method date. Why can that not be an instance method?
When making custom classes what's the logic in deciding whether a function works on an instance or the class itself?


  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,692Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2013
    It depends on the needs of what the method does. Let's look at your example of NSDate's +date method. What this method does is create a new date object with a time of right now. Usage might look like this:
    NSDate *rightNow = [NSDate date];
    You ask why this cannot be an instance method. Well, the purpose of this method is to create an instance. If you already have a date instance, why would you need to create another one? What would this method do on an existing date?
    NSDate *aDate = [[NSDate alloc] init];
    [aDate date];  // ?????
    What would you expect this to do?

    You can sort of think of class methods as utility methods. They can appear pretty much anywhere, but it is helpful to place them in classes that are performing related functions. If I really wanted to, I could make a +date method in NSArray that did exactly the same thing. But then this code would be confusing:
    NSDate *rightNow = [NSArray date];
    The end result would be exactly the same, but this just looks weird. Why the heck would an array class create a date object? Doesn't make sense. So class methods don't have to be in any particular class, but from an organizational and usage standpoint, they probably should be. If you're going to be dealing with dates, then somewhere along the line NSDate.h will be imported, and thus you gain access to a bunch of date-related capabilities.

    Instance methods, on the other hand, apply to something in particular. Let's take NSArray's -count method for example. You want the count for a particular array, like so:
    NSArray *oneArray = @[ @"A", @"B", @"C" ];
    NSArray *otherArray = @[ @"D", @"E" ];
    NSLog(@"%d items", [oneArray count]);
    I had two possible arrays to query, but I selected the specific one on which to call the count method. If count was a class method, then it would look like this:
    [NSArray count];  // ?????
    What would this do? Which array are you counting? All of them? That's going to be a big number...

    So in many, possibly even most, cases the nature of the method you are implementing will determine whether it should be a class method or an instance method. For remaining cases, you probably just use your judgement. Let's take a date example again, -laterDate:, which would look like this:
    NSDate *date1 = ...
    NSDate *date2 = ...
    NSDate *laterDate = [date1 laterDate:date2];
    Because this is an instance method, you already know that one of the parameters will be the instance that you are calling. So only one more parameter is needed. But this could certainly be recreated as a class method, like so:
    NSDate *date1 = ...
    NSDate *date2 = ...
    NSDate *laterDate = [NSDate laterDateFromFirstDate:date1 secondDate:date2];
    This way is a little wordier, and you have to explicitly account for each parameter. And though the implementation would be different, it would most likely be similar, and thus does not necessarily persuade one to go in either direction. The output would be exactly the same, so this is a style and judgement call.

    So it's hard to provide an overall guideline beyond the obvious: does your method apply to specific instances or not? If yes, then instance method. If not, probably could be a class method.
    Post edited by BrianSlick on
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • ryantcbryantcb Posts: 667Registered Users @ @ @
    Excellent thanks Brian
Sign In or Register to comment.