views:

149

answers:

4

I'm about to create a function which adds two NSDateComponents together is there any advantage to putting this in a C style function or should it go in a Obj-C method?

Is there ever a reason to use one rather then the other or should I always stick to Obj-C?

BTW: Not that it makes any difference I'm sure but this is for an app on the iPhone

Many thanks

A: 

I'm not clear on what you mean by "adds to NSDateComponents together". Could you explain exactly what you need to do?

Whether to make it a function or a method is largely a question of how you expect to use it. Is there some particular object that needs to do this, or is the need likely to show up in many places in your program?

NSResponder
+3  A: 

Function calls have slightly less overhead than Objective C method calls, but that's not a good reason to use them unless you have already detected a critical performance problem in exactly that area.

Objective C programs look very different from plain C programs. Maintaining consistency in the look and the idiom of the language is a good enough reason to implement almost all simple behaviours as methods rather than as C functions.

Take a look at where Cocoa-Touch uses functions rather than methods for an example of this approach.

Paul Lynch
+8  A: 

In Objective-C, the proper terminology is not to “call a method on” an object, but to “send a message to” an object.

That makes the decision easy: Are you sending a message to an object? Are you telling it to do something or asking it for information? If so, then make the object respond to that message by putting the code in a method. If not (if the task is independent of any object), then make it a function.

You might browse the Foundation functions, UIKit functions, and AppKit functions lists for examples of the sort of object-independence that makes something belong in a function rather than a method.

I'm about to create a function which adds to NSDateComponents together is there any advantage to putting this in a C style function or should it go in a Obj-C method?

I would put that into an instance method categoried onto NSDateComponents. The call (message) would look like [componentsA componentsByAddingComponents_PRH:componentsB]. (Note that I included my initials so that if Apple ever adds a componentsByAddingComponents: method of their own, my app's behavior won't suddenly change when users upgrade their devices to the new OS.)

One existing example of this is NSDecimalNumber's decimalNumberByAdding: method.

Peter Hosey
+1 for both "send a message to" (really really important distinction!) as well as how not everything's necessarily best done as a method (in a single dispatch language, at least).
Frank Shearar
+2  A: 

More likely you'll want to use an Objective-C method implemented as a category on NSDateComponents.

@interface NSDateComponents (DGCalculations)
/*!
 \brief    Add date components
 \details  Add the given date components to the receiver and
           return the result as a new date components object.
*/
- (NSDateComponents *)componentsByAddingComponents:(NSDateComponents *)components;
@end

@implementation NSDateComponents (DGCalculations)
- (NSDateComponents *)componentsByAddingComponents:(NSDateComponents *)components {
    NSDateComponents *result = [[[NSDateComponents alloc] init] autorelease];
    [result setDay:[self day] + [components day]];
    // handle the rest
    return result;
}
@end

The use then turns out to be a little nicer:

result = [firstComponents add:secondComponents];
wbyoung