Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
I am going through Apple's AnimatedTableView example.
It contains some code I haven't seen before, and I was wondering how this is working.

The ATColorCell.m file contains this code:
Code:
- (id)copyWithZone:(NSZone *)zone 
{
    self = [super copyWithZone:zone];
   [COLOR="Blue"] self->_color = [_color retain];[/COLOR] //??? 
    return self;
}

- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)frame ofView:(NSView *)controlView {
    NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
    NSRect colorRect = [self colorRectForFrame:frame];
    if (NSPointInRect(point, colorRect)) {
        // We combine in our own hit test marker
        return NSCellHitTrackableArea | ATCellHitTestColorRect;
    } else {
        NSUInteger result = [super hitTestForEvent:event inRect:[self _titleFrameForInteriorFrame:frame] ofView:controlView];
        // We don't want the label to be editable
        [COLOR="Blue"]result = result & ~NSCellHitEditableTextArea; [/COLOR]//??? ~ is negate in math
        return result;
    }   
}

Why not do or something similar?
Code:
 (id)copyWithZone:(NSZone *)zone 
{
    id newobject = [super copyWithZone:zone];
    [newobject setColor:[self color]]; 
    return newobject;
}
 
In addition to being the indirect struct member operator in C, the '->' operator, when used inside Objective-C object, allows direct access to the instance variables. For example:

Code:
@interface SomeClass : NSObject
@property (readonly) UIColor *color;
@end

@implementation SomeClass
@synthesize color = _color;

- (void)someMethod
{
    // Directly accesses the _color instance variable instead of
    // going through the mutator/accessor method
    self->_color = [[UIColor redColor] retain];

    NSLog(@"%@",[self->_color description]);

    [self->_color release], self->_color = nil;
}
@end
 
Last edited:
Yes, and that tells something about what an Obj C object actuall is. Dereferencing self and then use a the dot syntax should work equally well i.e: (*self)._color
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.