views:

21

answers:

1

Can someone explain to me when it is necessary to define your ivars as pointers?

I've seen many code samples that don't use pointers, while others do.

+1  A: 

The ivars definition depends on the type of the instance variables.

As a rule of thumb (for general purposes):

  • For primitive types (BOOL, short, int or long) or structures (NSRange, CGRect, etc), you should directly use their values (no pointer). Assignment is done by value-copy.
  • For class instances, you should use a pointer to reference them.

@interface ClassName : NSObject
{
    // instance variable declarations

    float width;
    float height;
    BOOL filled;
    CGRect rect;

    NSColor *fillColor;
    NSView *view;
}

// method declarations

@end
Laurent Etiemble