what do ~ and -> mean/do?

Discussion in 'Mac Programming' started by MrFusion, Aug 27, 2011.

  1. MrFusion macrumors 6502a

    Jun 8, 2005
    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:
    - (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?
     (id)copyWithZone:(NSZone *)zone 
        id newobject = [super copyWithZone:zone];
        [newobject setColor:[self color]]; 
        return newobject;
  2. gnasher729 macrumors P6


    Nov 25, 2005

    is the latest version of the C Standard which will explain what ~ and -> do.
  3. subsonix macrumors 68040

    Feb 2, 2008
    The tilde ~ is a bitwise NOT, so paired with the AND it will store the result in the result variable in the example.


    -> is used to reach struct members from a pointer to struct in C, and since valid C is valid Obj C, that is what this is.
  4. Guiyon, Aug 27, 2011
    Last edited: Aug 27, 2011

    Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    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:

    @interface SomeClass : NSObject
    @property (readonly) UIColor *color;
    @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;
  5. subsonix macrumors 68040

    Feb 2, 2008
    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

Share This Page