what do ~ and -> mean/do?

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

  1. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    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;
    }
    
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

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

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

    http://en.wikipedia.org/wiki/Bitwise_operation#NOT

    -> 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

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #4
    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
    
     
  5. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #5
    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