Using dot syntax for non-@property properties?

  • One of the dot syntax "Usage Summaries" from "The Objective-C 2.0
    Programming Language" is:

    xOrigin = aView.bounds.origin.x;

    This leads me to believe that I can replace the following code:

    -(void)viewClicked:(id)sender{
        NSSize size = [sender bounds].size;
        size.height *= 2;
        [[sender animator] setBoundsSize:size];
    }

    with something like:

    -(void)viewClicked:(id)sender{
        NSSize size = sender.bounds.size;
        size.height *= 2;
        sender.animator.boundsSize = size;
    }

    However, when I do so, I get errors like "request for member 'bounds'
    in something not a structure or a union". It sounds to me like the
    compiler has no idea what's going on with the dot there. Or, at least,
    it's definitely not [sender bounds], which is what I would have
    expected. What am I overlooking here?

    Many thanks,
    -Joshua Emmons
  • On Nov 6, 2007, at 5:30 PM, Joshua Emmons wrote:

    > What am I overlooking here?
    >
    <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articl
    es/chapter_5_section_4.html
    >
    (id)

    mmalc
  • On $BJ?@.(B 19/11/07, at 10:30, Joshua Emmons wrote:

    > One of the dot syntax "Usage Summaries" from "The Objective-C 2.0
    > Programming Language" is:
    >
    > xOrigin = aView.bounds.origin.x;
    >
    > This leads me to believe that I can replace the following code:
    >
    > -(void)viewClicked:(id)sender{
    > NSSize size = [sender bounds].size;
    > size.height *= 2;
    > [[sender animator] setBoundsSize:size];
    > }
    >
    > with something like:
    >
    > -(void)viewClicked:(id)sender{
    > NSSize size = sender.bounds.size;
    > size.height *= 2;
    > sender.animator.boundsSize = size;
    > }
    >
    > However, when I do so, I get errors like "request for member
    > 'bounds' in something not a structure or a union". It sounds to me
    > like the compiler has no idea what's going on with the dot there.
    > Or, at least, it's definitely not [sender bounds], which is what I
    > would have expected. What am I overlooking here?
    You can declare a property in a category, like so:

    @interface NSView (Properties)
    @property (assign) NSRect bounds;
    @end

    @interface NSView (Properties)
    @dynamic bounds;
    @end

    As long as there is a <key> and set<key> method pair, you can "tack"
    on a property.
    I've done it for some pre-leopard objects such as a subviews property
    etc on NSView...

    Andre

    >
    > Many thanks,
    > -Joshua Emmons
  • > You can declare a property in a category, like so:
    >
    > @interface NSView (Properties)
    > @property (assign) NSRect bounds;
    > @end
    >
    > @interface NSView (Properties)
    > @dynamic bounds;
    > @end
    >
    > As long as there is a <key> and set<key> method pair, you can "tack"
    > on a property.
    > I've done it for some pre-leopard objects such as a subviews
    > property etc on NSView...

    You don't need to tack on an explicit property declaration. The dot-
    syntax will work for any getter/setter pair, as long as the receiving
    object is statically typed.  In the original example, sender is
    declared as "id," so it doesn't work.

    Ali
  • On $BJ?@.(B 19/11/07, at 11:32, Ali Ozer wrote:

    >> You can declare a property in a category, like so:
    >>
    >> @interface NSView (Properties)
    >> @property (assign) NSRect bounds;
    >> @end
    >>
    >> @interface NSView (Properties)
    >> @dynamic bounds;
    >> @end
    >>
    >> As long as there is a <key> and set<key> method pair, you can
    >> "tack" on a property.
    >> I've done it for some pre-leopard objects such as a subviews
    >> property etc on NSView...
    >
    > You don't need to tack on an explicit property declaration. The dot-
    > syntax will work for any getter/setter pair, as long as the
    > receiving object is statically typed.  In the original example,
    > sender is declared as "id," so it doesn't work.
    Thats nice to know. Thanks.

    Andre

    >
    > Ali
    >
    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today